#!/bin/bash

set -e

export GREEN='\033[0;32m'
export YELLOW='\033[1;33m'
export RED='\033[0;31m'
export NC='\033[0m'

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

warn() {
    echo -e "${YELLOW}[WARN] $1${NC}"
}

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

check_command() {
    if ! command -v "$1" &> /dev/null; then
        error "Command $1 not found. Please install it first."
    fi
}

check_dependencies() {
    info "Checking dependencies..."
    check_command "podman"
    check_command "podman-compose"
    check_command "flutter"
    check_command "mvn"
    info "All dependencies are installed."
}

start_local_env() {
    info "Starting local development environment..."
    
    if [ ! -f "infrastructure/compose/podman-compose.local.yml" ]; then
        error "Compose file not found!"
    fi
    
    podman-compose -f infrastructure/compose/podman-compose.local.yml up -d
    
    info "Waiting for services to start..."
    sleep 30
    
    info "Checking service health..."
    podman ps --filter "health=healthy"
    
    info "Local environment started successfully!"
}

stop_local_env() {
    info "Stopping local development environment..."
    podman-compose -f infrastructure/compose/podman-compose.local.yml down
    info "Local environment stopped."
}

build_frontend() {
    info "Building frontend applications..."
    
    info "Building user_app..."
    cd frontend/user_app
    flutter pub get
    flutter analyze
    flutter test
    flutter build web --release --base-href /user/
    cd ../..
    
    info "Building rider_app..."
    cd frontend/rider_app
    flutter pub get
    flutter analyze
    flutter test
    flutter build web --release --base-href /rider/
    cd ../..
    
    info "Building platform_web..."
    cd frontend/platform_web
    npm ci
    npm run lint
    npm run typecheck
    npm run build
    cd ../..
    
    info "Building merchant_web..."
    cd frontend/merchant_web
    npm ci
    npm run lint
    npm run typecheck
    npm run build
    cd ../..
    
    info "All frontend applications built successfully!"
}

build_backend() {
    info "Building backend services..."
    cd services
    mvn clean install -DskipTests
    cd ..
    info "Backend built successfully!"
}

deploy_frontend() {
    info "Deploying frontend to local Nginx..."
    
    cp -r frontend/user_app/build/web/* infrastructure/nginx/html/user/
    cp -r frontend/rider_app/build/web/* infrastructure/nginx/html/rider/
    cp -r frontend/platform_web/dist/* infrastructure/nginx/html/platform/
    cp -r frontend/merchant_web/dist/* infrastructure/nginx/html/merchant/
    
    info "Frontend deployed successfully!"
}

run_tests() {
    info "Running all tests..."
    
    info "Running Flutter tests..."
    cd frontend/user_app
    flutter test --coverage
    cd ../rider_app
    flutter test --coverage
    cd ../..
    
    info "Running backend tests..."
    cd services
    mvn test
    cd ..
    
    info "All tests completed!"
}

show_help() {
    echo "Usage: $0 [COMMAND]"
    echo ""
    echo "Commands:"
    echo "  check          Check dependencies"
    echo "  start          Start local development environment"
    echo "  stop           Stop local development environment"
    echo "  build-frontend Build all frontend applications"
    echo "  build-backend  Build backend services"
    echo "  build-all      Build everything"
    echo "  deploy         Deploy frontend to local Nginx"
    echo "  test           Run all tests"
    echo "  full           Run full workflow: check -> start -> build-all -> deploy -> test"
    echo "  help           Show this help message"
}

main() {
    case "$1" in
        check)
            check_dependencies
            ;;
        start)
            start_local_env
            ;;
        stop)
            stop_local_env
            ;;
        build-frontend)
            build_frontend
            ;;
        build-backend)
            build_backend
            ;;
        build-all)
            build_frontend
            build_backend
            ;;
        deploy)
            deploy_frontend
            ;;
        test)
            run_tests
            ;;
        full)
            check_dependencies
            start_local_env
            build_frontend
            deploy_frontend
            run_tests
            ;;
        help)
            show_help
            ;;
        *)
            error "Unknown command: $1. Use 'help' for available commands."
            ;;
    esac
}

main "$@"