#!/bin/bash
# Comprehensive Setup Script for PunimTag Photo Viewer
# This script handles the complete setup process including:
# - Dependency installation
# - Prisma client generation
# - Database setup
# - Admin user creation

set -e  # Exit on error

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR" && pwd)"

cd "$PROJECT_DIR"

echo "🚀 PunimTag Photo Viewer - Complete Setup"
echo "========================================"
echo ""

# Step 1: Install dependencies
echo "📦 Step 1: Installing dependencies..."
if [ -f "scripts/install-dependencies.sh" ]; then
    ./scripts/install-dependencies.sh
else
    echo "   Running npm install..."
    npm install
    echo "   Generating Prisma clients..."
    npm run prisma:generate:all
fi
echo ""

# Step 2: Check for .env file
echo "📋 Step 2: Checking environment configuration..."
if [ ! -f ".env" ]; then
    echo "⚠️  .env file not found!"
    echo ""
    echo "   Please create a .env file with the following variables:"
    echo "   - DATABASE_URL (required)"
    echo "   - DATABASE_URL_AUTH (optional, for authentication)"
    echo "   - DATABASE_URL_WRITE (optional, for write operations)"
    echo "   - NEXTAUTH_SECRET (required for authentication)"
    echo "   - NEXTAUTH_URL (required for authentication)"
    echo ""
    read -p "Continue with database setup anyway? (y/N) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        echo "Setup cancelled. Please create .env file and run again."
        exit 1
    fi
else
    echo "✅ .env file found"
    # Load environment variables
    export $(grep -v '^#' .env | xargs)
fi
echo ""

# Step 3: Database setup
echo "🗄️  Step 3: Setting up database..."
if [ -z "$DATABASE_URL_AUTH" ]; then
    echo "⚠️  DATABASE_URL_AUTH not set in .env"
    echo "   Skipping auth database setup"
    echo "   (Authentication features will not be available)"
else
    echo "   Setting up auth database tables and admin user..."
    npx tsx scripts/setup-database.ts || {
        echo "⚠️  Database setup encountered an error"
        echo "   This might be normal if tables already exist"
        echo "   Continuing..."
    }
fi
echo ""

# Step 4: Verify setup
echo "✅ Step 4: Verifying setup..."
echo "   Checking Prisma clients..."
if [ -d "node_modules/.prisma/client" ]; then
    echo "   ✅ Main Prisma client found"
else
    echo "   ⚠️  Main Prisma client not found, generating..."
    npm run prisma:generate
fi

if [ -d "node_modules/.prisma/client-auth" ]; then
    echo "   ✅ Auth Prisma client found"
else
    if [ -n "$DATABASE_URL_AUTH" ]; then
        echo "   ⚠️  Auth Prisma client not found, generating..."
        npm run prisma:generate:auth
    fi
fi
echo ""

# Step 5: Test database connection (optional)
if [ -n "$DATABASE_URL" ]; then
    echo "🔍 Step 5: Testing database connection..."
    if npm run check:permissions > /dev/null 2>&1; then
        echo "   ✅ Database connection successful"
    else
        echo "   ⚠️  Database connection test failed"
        echo "   Run 'npm run check:permissions' manually to diagnose"
    fi
    echo ""
fi

echo "========================================"
echo "✅ Setup complete!"
echo ""
echo "Next steps:"
echo "1. Verify your .env file is properly configured"
echo "2. Run 'npm run dev' to start the development server"
echo "3. Visit http://localhost:3001 in your browser"
echo ""
echo "For more information, see README.md"
echo ""
