#!/bin/bash
set -e

# ── Edit these variables before running ─────────────────────────
PROJECT_DIR="alenayah"                                          # folder name inside public_html
REPO_URL="https://github.com/Tech-Time-Websites/Alenayah.git"   # repo URL — if private, git will prompt for credentials interactively
APP_URL="https://demo.techtime.me/alenayah"                      # full site URL on the demo server
DB_HOST="127.0.0.1"

# Create these first from cPanel → "MySQL® Databases" (Create Database, Add New User,
# Add User to Database with ALL PRIVILEGES), then paste the exact names/password cPanel
# gave you below. Shared hosting has no root MySQL access, so this can't be automated.
DB_DATABASE="demotechtime_alenayah"
DB_USERNAME="demotechtime_alenayah_app"
DB_PASSWORD="fT]B)q[l@DZ2BpD*"
# ─────────────────────────────────────────────────────────────

echo "==> [1/8] Cloning the project"
if [ -d "$PROJECT_DIR/.git" ]; then
    echo "Folder already exists, pulling latest instead of cloning"
    cd "$PROJECT_DIR"
    git pull
else
    git clone "$REPO_URL" "$PROJECT_DIR"
    cd "$PROJECT_DIR"
fi

echo "==> [2/8] Installing composer dependencies"
composer install --no-dev --optimize-autoloader

echo "==> [3/8] Verifying database credentials"
if [ "$DB_DATABASE" = "CHANGE_ME" ] || [ "$DB_USERNAME" = "CHANGE_ME" ] || [ "$DB_PASSWORD" = "CHANGE_ME" ]; then
    echo "ERROR: set DB_DATABASE / DB_USERNAME / DB_PASSWORD at the top of this script"
    echo "       (create them first from cPanel -> MySQL Databases)"
    exit 1
fi
mysql -u"$DB_USERNAME" -p"$DB_PASSWORD" -h"$DB_HOST" -e "SELECT 1;" "$DB_DATABASE" >/dev/null
echo "Connected to '${DB_DATABASE}' as '${DB_USERNAME}' OK"

echo "==> [4/8] Setting up .env"
if [ ! -f .env ]; then
    cp .env.example .env
fi
sed -i "s|^APP_ENV=.*|APP_ENV=production|" .env
sed -i "s|^APP_DEBUG=.*|APP_DEBUG=false|" .env
sed -i "s|^APP_URL=.*|APP_URL=${APP_URL}|" .env
sed -i "s|^DB_DATABASE=.*|DB_DATABASE=${DB_DATABASE}|" .env
sed -i "s|^DB_USERNAME=.*|DB_USERNAME=${DB_USERNAME}|" .env
sed -i "s|^DB_PASSWORD=.*|DB_PASSWORD=${DB_PASSWORD}|" .env
sed -i "s|^DB_HOST=.*|DB_HOST=${DB_HOST}|" .env

if ! grep -q "^APP_KEY=base64:" .env; then
    php artisan key:generate --force
fi

echo "==> [5/8] Setting permissions"
chmod -R 755 storage bootstrap/cache

echo "==> [6/8] Importing the database (db.sql)"
if [ -f db.sql ]; then
    mysql -u"$DB_USERNAME" -p"$DB_PASSWORD" -h"$DB_HOST" "$DB_DATABASE" < db.sql
    echo "db.sql imported"
else
    echo "db.sql not found — skipped this step"
fi

echo "==> [7/8] Building assets (Vite)"
if command -v npm >/dev/null 2>&1; then
    npm ci
    npm run build
else
    echo "npm not available on this server — build public/build locally and upload it manually"
fi

echo "==> [8/8] storage link + caching"
php artisan storage:link || true
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan event:cache

echo ""
echo "==> Done!"
echo "==> Check manually:"
echo "  - Upload storage/app/public/uploads images (gitignored, not cloned)"
echo "  - Open ${APP_URL} in the browser"
echo "  - Check storage/logs/laravel.log for any errors"
