#!/bin/bash

# Simple script to run backup web app in a persistent screen session

SESSION_NAME="backup-web-app"
APP_DIR="/home/acedanger/shell"
PYTHON_CMD="python3"

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

print_status() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

check_screen() {
    if ! command -v screen &> /dev/null; then
        print_error "Screen is not installed. Install it with: sudo apt install screen"
        exit 1
    fi
}

start_app() {
    check_screen
    
    # Check if session already exists
    if screen -list | grep -q "$SESSION_NAME"; then
        print_warning "Session '$SESSION_NAME' already exists"
        print_status "Use './run-backup-web-screen.sh status' to check or './run-backup-web-screen.sh stop' to stop"
        exit 1
    fi
    
    print_status "Starting backup web app in screen session '$SESSION_NAME'..."
    
    # Start new detached screen session
    cd "$APP_DIR" || exit 1
    screen -dmS "$SESSION_NAME" bash -c "
        export BACKUP_ROOT=/mnt/share/media/backups
        export FLASK_ENV=production
        $PYTHON_CMD backup-web-app.py
    "
    
    sleep 2
    
    if screen -list | grep -q "$SESSION_NAME"; then
        print_status "✅ Backup web app started successfully!"
        print_status "Session: $SESSION_NAME"
        print_status "URL: http://localhost:5000"
        print_status ""
        print_status "Commands:"
        print_status "  View logs: ./run-backup-web-screen.sh logs"
        print_status "  Stop app:  ./run-backup-web-screen.sh stop"
        print_status "  Status:    ./run-backup-web-screen.sh status"
    else
        print_error "Failed to start the application"
        exit 1
    fi
}

stop_app() {
    if screen -list | grep -q "$SESSION_NAME"; then
        print_status "Stopping backup web app..."
        screen -S "$SESSION_NAME" -X quit
        print_status "✅ Application stopped"
    else
        print_warning "No session '$SESSION_NAME' found"
    fi
}

status_app() {
    if screen -list | grep -q "$SESSION_NAME"; then
        print_status "✅ Backup web app is running"
        print_status "Session details:"
        screen -list | grep "$SESSION_NAME"
        print_status ""
        print_status "Access the session with: screen -r $SESSION_NAME"
        print_status "Detach from session with: Ctrl+A, then D"
    else
        print_warning "❌ Backup web app is not running"
    fi
}

show_logs() {
    if screen -list | grep -q "$SESSION_NAME"; then
        print_status "Connecting to session '$SESSION_NAME'..."
        print_status "Press Ctrl+A, then D to detach from the session"
        screen -r "$SESSION_NAME"
    else
        print_error "No session '$SESSION_NAME' found. App is not running."
    fi
}

restart_app() {
    print_status "Restarting backup web app..."
    stop_app
    sleep 2
    start_app
}

show_help() {
    echo "Backup Web App Screen Manager"
    echo
    echo "Usage: $0 {start|stop|restart|status|logs|help}"
    echo
    echo "Commands:"
    echo "  start    - Start the app in a screen session"
    echo "  stop     - Stop the app"
    echo "  restart  - Restart the app"
    echo "  status   - Check if app is running"
    echo "  logs     - Connect to the screen session to view logs"
    echo "  help     - Show this help message"
}

case "${1:-}" in
    start)
        start_app
        ;;
    stop)
        stop_app
        ;;
    restart)
        restart_app
        ;;
    status)
        status_app
        ;;
    logs)
        show_logs
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        print_error "Invalid command: ${1:-}"
        echo
        show_help
        exit 1
        ;;
esac
