#!/bin/bash

# Script untuk deployment SIDOHIBAH di server
# Jalankan dengan: bash deploy.sh

echo "=== SIDOHIBAH DEPLOYMENT SCRIPT ==="
echo "Starting deployment process..."

# 1. Install dependencies
echo "1. Installing dependencies..."
composer install --no-dev --optimize-autoloader

# 2. Setup environment
echo "2. Setting up environment..."
if [ ! -f .env ]; then
    cp .env.example .env
    echo "Environment file created. Please configure database settings in .env"
    echo "Then run: php artisan key:generate"
    exit 1
fi

# 3. Generate key if not exists
echo "3. Generating application key..."
php artisan key:generate

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

# 5. Seed basic data
echo "5. Seeding basic data..."
php artisan db:seed --class=KategoriSeeder --force
php artisan db:seed --class=UserSeeder --force
php artisan db:seed --class=SchoolSeeder --force

# 6. Create storage link
echo "6. Creating storage link..."
php artisan storage:link

# 7. Set permissions
echo "7. Setting permissions..."
chmod -R 775 storage
chmod -R 775 bootstrap/cache

# 8. Clear and cache
echo "8. Optimizing application..."
php artisan config:cache
php artisan route:cache
php artisan view:cache

# 9. Ask for dummy data
echo ""
echo "=== DEPLOYMENT COMPLETED ==="
echo ""
read -p "Do you want to create dummy pengajuan data? (y/n): " create_dummy

if [ "$create_dummy" = "y" ] || [ "$create_dummy" = "Y" ]; then
    read -p "How many dummy pengajuan to create? (default: 20): " dummy_count
    dummy_count=${dummy_count:-20}
    
    echo "Creating $dummy_count dummy pengajuan..."
    php artisan pengajuan:create-dummy --count=$dummy_count
    echo "Dummy data created successfully!"
fi

echo ""
echo "=== DEPLOYMENT FINISHED ==="
echo "Your SIDOHIBAH application is ready!"
echo ""
echo "Next steps:"
echo "1. Configure your web server (Apache/Nginx)"
echo "2. Set up SSL certificate if needed"
echo "3. Configure cron jobs for Laravel scheduler"
echo "4. Test the application"
echo ""
echo "For cron jobs, add this to your crontab:"
echo "* * * * * cd /path/to/your/project && php artisan schedule:run >> /dev/null 2>&1"
