#!/bin/bash
# =============================================================
# Ataya Platform – Production Deployment Script
# =============================================================
set -e

echo ""
echo "╔══════════════════════════════════════════╗"
echo "║    Ataya Platform – Deployment Script    ║"
echo "╚══════════════════════════════════════════╝"
echo ""

APP_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$APP_DIR"

# ── Check PHP ────────────────────────────────────────────────
echo "▸ PHP Version:"
php -v | head -1

# ── Check artisan exists ─────────────────────────────────────
if [ ! -f "artisan" ]; then
    echo "✗ artisan not found. Make sure you are in the project root."
    exit 1
fi

# ── Install dependencies ─────────────────────────────────────
echo ""
echo "▸ Installing Composer dependencies..."
if [ ! -f "vendor/autoload.php" ]; then
    composer install --no-dev --optimize-autoloader --no-interaction
else
    composer install --no-dev --optimize-autoloader --no-interaction
fi

# ── .env setup ───────────────────────────────────────────────
if [ ! -f ".env" ]; then
    echo ""
    echo "▸ Creating .env from example..."
    cp .env.example .env
    php artisan key:generate
    echo ""
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    echo "  ⚠  .env file created. Please edit it now:"
    echo "     nano .env"
    echo ""
    echo "  Required settings:"
    echo "    DB_HOST, DB_DATABASE, DB_USERNAME, DB_PASSWORD"
    echo "    MAIL_*, STRIPE_*, PAYPAL_*, ZEFFY_*"
    echo ""
    echo "  After editing, run this script again:"
    echo "     bash deploy.sh"
    echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    exit 0
fi

# ── Storage permissions ──────────────────────────────────────
echo ""
echo "▸ Setting storage permissions..."
chmod -R 775 storage bootstrap/cache 2>/dev/null || true

# ── Run migrations ───────────────────────────────────────────
echo ""
echo "▸ Running database migrations..."
php artisan migrate --force

# ── Seed production data ─────────────────────────────────────
echo ""
echo "▸ Seeding production data (plans + roles + super-admin)..."
php artisan db:seed --class=ProductionSeeder --force

# ── Cache optimization ───────────────────────────────────────
echo ""
echo "▸ Optimizing for production..."
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

# ── Storage link ─────────────────────────────────────────────
echo ""
echo "▸ Creating storage symlink..."
php artisan storage:link 2>/dev/null || echo "  (storage link already exists)"

# ── Ownership (cPanel/VPS) ───────────────────────────────────
echo ""
echo "▸ Fixing ownership..."
chown -R www-data:www-data storage bootstrap/cache 2>/dev/null \
    || chown -R $(whoami) storage bootstrap/cache 2>/dev/null \
    || true

echo ""
echo "✅ Deployment complete!"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  Super Admin Login:"
echo "    Email:    admin@ataya.com"
echo "    Password: Ataya@2025!"
echo "  ⚠  Change password after first login!"
echo ""
echo "  Optional: Load demo data:"
echo "    php artisan db:seed --class=DemoSeeder"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
