#!/bin/bash
set -e

echo "Starting TimeTrack application (PostgreSQL-only mode)..."

# Wait for PostgreSQL to be ready
echo "Waiting for PostgreSQL to be ready..."
while ! pg_isready -h db -p 5432 -U "$POSTGRES_USER" > /dev/null 2>&1; do
    echo "PostgreSQL is not ready yet. Waiting..."
    sleep 2
done
echo "PostgreSQL is ready!"

# Run Flask-Migrate migrations
echo ""
echo "=== Running Database Migrations ==="
export FLASK_APP=app.py

# Check if migrations directory exists
if [ -d "migrations" ]; then
    echo "Applying database migrations..."
    flask db upgrade
    if [ $? -ne 0 ]; then
        echo "❌ Migration failed! Check the logs above."
        exit 1
    fi
    echo "✅ Database migrations completed successfully"
else
    echo "⚠️  No migrations directory found. Initializing Flask-Migrate..."
    
    # Use Docker-friendly initialization (no Git required)
    python docker_migrate_init.py
    if [ $? -ne 0 ]; then
        echo "❌ Migration initialization failed!"
        exit 1
    fi
    
    # Check if database has existing tables
    python -c "
from app import app, db
with app.app_context():
    inspector = db.inspect(db.engine)
    tables = [t for t in inspector.get_table_names() if t != 'alembic_version']
    if tables:
        print('has_tables')
" > /tmp/db_check.txt
    
    if grep -q "has_tables" /tmp/db_check.txt 2>/dev/null; then
        echo "📊 Existing database detected. Marking as current..."
        flask db stamp head
        echo "✅ Database marked as current"
    else
        echo "🆕 Empty database detected. Creating tables..."
        flask db upgrade
        echo "✅ Database tables created"
    fi
    
    rm -f /tmp/db_check.txt
fi

# Sync PostgreSQL enums with Python models
echo ""
echo "=== Syncing PostgreSQL Enums ==="
python sync_postgres_enums.py
if [ $? -ne 0 ]; then
    echo "⚠️  Enum sync failed, but continuing..."
fi

# Legacy migration support (can be removed after full transition)
if [ -f "migrations_old/run_postgres_migrations.py" ]; then
    echo ""
    echo "=== Checking Legacy Migrations ==="
    echo "Found old migration system. Consider removing after confirming Flask-Migrate is working."
fi

# Start cron service for scheduled tasks
echo ""
echo "=== Starting Cron Service ==="
service cron start
if [ $? -eq 0 ]; then
    echo "✅ Cron service started for scheduled cleanup tasks"
else
    echo "⚠️  Failed to start cron service, cleanup tasks won't run automatically"
fi

# Start the Flask application with gunicorn
echo ""
echo "=== Starting Application ==="
echo "Starting Flask application with gunicorn..."
exec gunicorn --bind 0.0.0.0:5000 --workers 4 --threads 2 --timeout 30 app:app