#!/bin/bash

# Project Management Script for ML Experiment Manager
# Provides unified interface for managing all components

set -euo pipefail

make_target_exists() {
    make -n "$1" >/dev/null 2>&1
}

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'

print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

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

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

print_header() {
    echo -e "${PURPLE}$1${NC}"
}

print_app() {
    echo -e "${CYAN}$1${NC}"
}

show_status() {
    print_header "ML Experiment Manager Status"
    echo "=================================="
    echo ""
    
    # Check Go apps
    print_app "Go Applications:"
    local go_apps=("api-server" "worker" "tui")
    for app in "${go_apps[@]}"; do
        if [[ -f "bin/$app" ]]; then
            echo "  ✅ $app: Built"
        else
            echo "  ❌ $app: Not built"
        fi
    done
    echo ""
    
    # Check Zig CLI
    print_app "Zig CLI:"
    if [[ -f "cli/zig-out/bin/ml" ]]; then
        echo "  ✅ CLI: Built"
    else
        echo "  ❌ CLI: Not built"
    fi
    echo ""
    
    # Check services
    print_app "Services:"
    if command -v redis-cli &> /dev/null; then
        if redis-cli ping | grep -q "PONG"; then
            echo "  ✅ Redis: Running"
        else
            echo "  ⚠️  Redis: Not running"
        fi
    else
        echo "  ❌ Redis: Not installed"
    fi
    
    if command -v docker &> /dev/null; then
        echo "  ✅ Docker: Available"
    else
        echo "  ❌ Docker: Not installed"
    fi
    echo ""
    
    # Check configuration
    print_app "Configuration:"
    if [[ -f "configs/api/dev.yaml" ]]; then
        echo "  ✅ Security config: Found"
    else
        echo "  ⚠️  Security config: Not found"
    fi
    
    if [[ -f ".env.dev" ]]; then
        echo "  ✅ Environment: Found"
    else
        echo "  ⚠️  Environment: Not found"
    fi
    
    if [[ -f "ssl/cert.pem" && -f "ssl/key.pem" ]]; then
        echo "  ✅ SSL certificates: Found"
    else
        echo "  ⚠️  SSL certificates: Not found"
    fi
    echo ""
}

build_all() {
    print_header "Building All Components"
    echo "============================="
    echo ""
    
    if command -v zig &> /dev/null; then
        print_info "Building all components (Go + Zig CLI)..."
        make build
    else
        print_warning "Zig not found, building Go components only"
        go build -o bin/api-server cmd/api-server/main.go
        go build -o bin/worker cmd/worker/worker_server.go
        go build -o bin/tui ./cmd/tui
    fi
    
    print_success "Build completed!"
}

test_all() {
    print_header "Running All Tests"
    echo "===================="
    echo ""
    
    if make_target_exists test-full; then
        print_info "Running full test suite..."
        make test-full
    else
        print_info "Running test suite..."
        make test
    fi
    
    print_success "All tests completed!"
}

start_services() {
    print_header "Starting Services"
    echo "==================="
    echo ""
    
    # Start Redis if available
    if command -v redis-server &> /dev/null; then
        if ! pgrep -f "redis-server" > /dev/null; then
            print_info "Starting Redis..."
            redis-server --daemonize yes --port 6379
            print_success "Redis started"
        else
            print_info "Redis already running"
        fi
    fi
    
    # Start API server if built
    if [[ -f "bin/api-server" ]]; then
        print_info "Starting API server..."
        if [[ -f "configs/api/dev.yaml" ]]; then
            ./bin/api-server --config configs/api/dev.yaml &
        else
            print_warning "No config found, using defaults"
            ./bin/api-server &
        fi
        print_success "API server started (PID: $!)"
    else
        print_error "API server not built. Run 'make build' first."
    fi
    
    print_success "Services started!"
}

check_health() {
    print_header "API Health Check"
    echo "=================="
    echo ""
    
    print_info "Checking if API port is open..."
    
    # First check if port 9101 is open
    if ! nc -z localhost 9101 2>/dev/null; then
        print_error "API port 9101 not open - is it running?"
        print_info "Start with: ./tools/manage.sh start"
        return 1
    fi
    
    print_info "Port 9101 is open, checking API health endpoint..."
    
    # Try the health endpoint
    local api_key_header=""
    if [[ -n "${FETCH_ML_API_KEY:-}" ]]; then
        api_key_header="-H X-API-Key: ${FETCH_ML_API_KEY}"
    fi

    response=$(curl -s --max-time 3 ${api_key_header} http://localhost:9101/health 2>/dev/null || true)
    if [[ -z "$response" ]]; then
        response=$(curl -k -s --max-time 3 ${api_key_header} https://localhost:9101/health 2>/dev/null || true)
    fi
    
    if [[ "$response" == "OK" ]]; then
        print_success "API is healthy: $response"
    elif [[ "$response" == *"IP not whitelisted"* ]]; then
        print_warning "API running but IP not whitelisted (expected behavior)"
        if [[ -n "${FETCH_ML_API_KEY:-}" ]]; then
            print_info "Try: curl -k -H 'X-API-Key: $FETCH_ML_API_KEY' https://localhost:9101/health"
        else
            print_info "Try: curl -k https://localhost:9101/health"
        fi
    else
        print_error "Unexpected response: $response"
    fi
}

stop_services() {
    print_header "Stopping Services"
    echo "=================="
    echo ""
    
    # Stop API server
    if pgrep -f "api-server" > /dev/null; then
        print_info "Stopping API server..."
        pkill -f "api-server"
        print_success "API server stopped"
    fi
    
    # Stop Redis
    if command -v redis-cli &> /dev/null; then
        print_info "Stopping Redis..."
        redis-cli shutdown 2>/dev/null || true
        print_success "Redis stopped"
    fi
    
    print_success "All services stopped!"
}

run_security() {
    print_header "Security Management"
    echo "===================="
    echo ""
    
    case "${1:-check}" in
        "check")
            print_info "Running security checks..."
            if make_target_exists security-check; then
                make security-check
            else
                print_warning "No 'security-check' Make target found"
                print_info "Try: make ci-local"
            fi
            ;;
        "monitor")
            print_info "Starting security monitoring..."
            if make_target_exists security-monitor; then
                make security-monitor
            else
                print_warning "No 'security-monitor' Make target found"
            fi
            ;;
        "deploy")
            print_info "Deploying with security..."
            if make_target_exists security-deploy; then
                make security-deploy
            else
                print_warning "No 'security-deploy' Make target found"
            fi
            ;;
        "audit")
            print_info "Running security audit..."
            if make_target_exists security-audit; then
                make security-audit
            else
                print_warning "No 'security-audit' Make target found"
            fi
            ;;
        *)
            echo "Usage: $0 security {check|monitor|deploy|audit}"
            exit 1
            ;;
    esac
}

run_development() {
    print_header "Development Environment"
    echo "========================="
    echo ""
    
    case "${1:-setup}" in
        "setup")
            print_info "Setting up development environment..."
            print_warning "Legacy setup scripts were removed; using Makefile/deployments instead"
            print_info "Try: make dev"
            print_info "Or: ./deployments/deploy.sh dev up"
            ;;
        "quick")
            print_info "Running quick start..."
            print_warning "Legacy quick start script was removed; using deployments instead"
            print_info "Try: ./deployments/deploy.sh dev up"
            ;;
        "deps")
            print_info "Installing dependencies..."
            if make_target_exists install-deps; then
                make install-deps
            else
                print_warning "No 'install-deps' Make target found"
            fi
            ;;
        *)
            echo "Usage: $0 dev {setup|quick|deps}"
            exit 1
            ;;
    esac
}

show_logs() {
    print_header "Application Logs"
    echo "=================="
    echo ""
    
    # Show application logs
    if [[ -f "logs/fetch_ml.log" ]]; then
        print_app "Application Log:"
        tail -20 logs/fetch_ml.log
        echo ""
    fi
    
    if [[ -f "logs/audit.log" ]]; then
        print_app "Security Log:"
        tail -20 logs/audit.log
        echo ""
    fi
    
    # Show Docker logs if running
    if command -v docker &> /dev/null; then
        local containers=$(docker ps --format "table {{.Names}}" | grep "ml-experiment" || true)
        if [[ -n "$containers" ]]; then
            print_app "Docker Logs:"
            docker logs --tail=20 $(echo "$containers" | tail -1) 2>/dev/null || true
        fi
    fi
}

cleanup() {
    print_header "Cleanup Project"
    echo "================"
    echo ""
    
    print_info "Cleaning project artifacts..."
    make clean
    
    print_info "Stopping services..."
    stop_services
    
    print_success "Cleanup completed!"
}

show_help() {
    print_header "Project Management Script"
    echo "==========================="
    echo ""
    echo "Usage: ./tools/manage.sh {status|build|test|start|stop|health|security|dev|logs|cleanup|help}"
    echo ""
    echo "Commands:"
    echo "  status    - Show project status"
    echo "  build     - Build all components"
    echo "  test      - Run all tests"
    echo "  start     - Start all services"
    echo "  stop      - Stop all services"
    echo "  health    - Check API health endpoint"
    echo "  security  - Security management (check|monitor|deploy|audit)"
    echo "  dev       - Development environment (setup|quick|deps)"
    echo "  logs      - Show application logs"
    echo "  cleanup   - Clean project artifacts and stop services"
    echo "  help      - Show this help"
    echo ""
    echo "Examples:"
    echo "  $0 status              # Show current status"
    echo "  $0 health              # Check API health"
    echo "  $0 build && $0 test    # Build and test everything"
    echo "  $0 start               # Start all services"
    echo "  $0 security monitor    # Start security monitoring"
    echo "  $0 dev setup           # Setup development environment"
    echo ""
    echo "Quick Start:"
    echo "  $0 dev setup && $0 start && $0 status"
}

# Main function
main() {
    case "${1:-help}" in
        "status")
            show_status
            ;;
        "build")
            build_all
            ;;
        "test")
            test_all
            ;;
        "start")
            start_services
            ;;
        "stop")
            stop_services
            ;;
        "health")
            check_health
            ;;
        "security")
            run_security "${2:-check}"
            ;;
        "dev")
            run_development "${2:-setup}"
            ;;
        "logs")
            show_logs
            ;;
        "cleanup")
            cleanup
            ;;
        "help"|"-h"|"--help")
            show_help
            ;;
        *)
            print_error "Unknown command: $1"
            echo "Use '$0 help' for usage information"
            exit 1
            ;;
    esac
}

# Run main function
main "$@"
