#!/bin/bash
########################################
# Install Phase 1 Cron Jobs
# Critical foundation cron jobs
########################################

echo "========================================="
echo "🚀 Installing Phase 1 Cron Jobs"
echo "========================================="
echo ""

# Detect environment
if [ -d "/home/globalgala/public_html/2026_backend_prod" ]; then
    PROJECT_ROOT="/home/globalgala/public_html/2026_backend_prod"
    echo "📍 Environment: Production Server"
else
    PROJECT_ROOT="/Users/charlie/code/showprima"
    echo "📍 Environment: Local Development"
fi

echo "📁 Project root: $PROJECT_ROOT"
echo ""

# Make scripts executable
echo "🔧 Making scripts executable..."
chmod +x "$PROJECT_ROOT/scripts/queue-worker-cron.sh"
chmod +x "$PROJECT_ROOT/scripts/health-check-monitor.sh"
chmod +x "$PROJECT_ROOT/scripts/disk-space-monitor.sh"
echo "✅ Scripts are executable"
echo ""

# Show current crontab
echo "📋 Current crontab:"
crontab -l 2>/dev/null || echo "(empty)"
echo ""

# Create backup of current crontab
echo "💾 Backing up current crontab..."
crontab -l > /tmp/crontab-backup-$(date +%Y%m%d-%H%M%S).txt 2>/dev/null
echo "✅ Backup created at /tmp/crontab-backup-*.txt"
echo ""

# Prepare new cron jobs
echo "📝 Preparing cron jobs..."
cat > /tmp/new-cron-jobs.txt << EOF
# ============================================================
# Global Gala - Phase 1 Critical Cron Jobs
# Installed: $(date)
# ============================================================

# CRITICAL: Laravel Scheduler (runs all scheduled tasks in Kernel.php)
# This enables: seat hold cleanup, payment cleanup, backups, cache cleanup, etc.
* * * * * cd $PROJECT_ROOT && php artisan schedule:run >> /dev/null 2>&1

# Queue worker monitoring (ensures emails are sent)
*/5 * * * * /bin/bash $PROJECT_ROOT/scripts/queue-worker-cron.sh

# System health check (API, DB, queue, errors)
*/5 * * * * /bin/bash $PROJECT_ROOT/scripts/health-check-monitor.sh

# Disk space monitoring (alerts before disk full)
*/5 * * * * /bin/bash $PROJECT_ROOT/scripts/disk-space-monitor.sh

EOF

echo "✅ Cron jobs prepared"
echo ""

# Show what will be installed
echo "========================================="
echo "📄 Cron jobs to be installed:"
echo "========================================="
cat /tmp/new-cron-jobs.txt
echo ""

# Ask for confirmation
read -p "Install these cron jobs? (y/n): " -n 1 -r
echo ""

if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo "❌ Installation cancelled"
    exit 1
fi

# Install cron jobs
echo ""
echo "🚀 Installing cron jobs..."

# Combine existing and new cron jobs
(crontab -l 2>/dev/null; cat /tmp/new-cron-jobs.txt) | crontab -

echo "✅ Cron jobs installed"
echo ""

# Verify installation
echo "========================================="
echo "✅ Verification"
echo "========================================="

echo ""
echo "📋 Current crontab:"
crontab -l | grep -A 20 "Global Gala"
echo ""

# Test scripts manually
echo "🧪 Testing scripts..."
echo ""

echo "1️⃣ Testing disk space monitor..."
bash "$PROJECT_ROOT/scripts/disk-space-monitor.sh"
echo ""

echo "2️⃣ Testing health check monitor..."
bash "$PROJECT_ROOT/scripts/health-check-monitor.sh"
echo ""

echo "3️⃣ Testing queue worker cron..."
bash "$PROJECT_ROOT/scripts/queue-worker-cron.sh"
echo ""

echo "========================================="
echo "✅ Phase 1 Installation Complete!"
echo "========================================="
echo ""
echo "📊 What was installed:"
echo "  ✅ Laravel scheduler (enables 15+ scheduled tasks)"
echo "  ✅ Queue worker monitoring (every 5 minutes)"
echo "  ✅ Health check monitoring (every 5 minutes)"
echo "  ✅ Disk space monitoring (every 5 minutes)"
echo ""
echo "📝 Log files:"
echo "  - $PROJECT_ROOT/storage/logs/queue-worker.log"
echo "  - $PROJECT_ROOT/storage/logs/health-check-monitor.log"
echo "  - $PROJECT_ROOT/storage/logs/disk-space-monitor.log"
echo "  - $PROJECT_ROOT/storage/logs/health-alerts.log"
echo "  - $PROJECT_ROOT/storage/logs/disk-space-alerts.log"
echo ""
echo "🔍 Monitor execution:"
echo "  tail -f $PROJECT_ROOT/storage/logs/health-check-monitor.log"
echo ""
echo "📧 Alerts will be sent to: admin@globalgala.com"
echo "   (Configure via .env: HEALTH_CHECK_ALERT_EMAIL, DISK_SPACE_ALERT_EMAIL)"
echo ""
echo "🎉 Your system is now monitored!"
echo "========================================="
