#!/bin/bash

# Database Management Master Script
# Central hub for all database operations and tools

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m' # No Color

# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname $(dirname "$SCRIPT_DIR"))"

# Change to project root
cd "$PROJECT_ROOT"

# Logging functions
log() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

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

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

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

print_header() {
    echo -e "${BLUE}${BOLD}=== $1 ===${NC}"
}

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

print_usage() {
    echo -e "${BOLD}Database Management Hub${NC}"
    echo
    echo "Usage: $0 <category> <command> [options]"
    echo
    echo -e "${BOLD}Categories:${NC}"
    echo
    echo -e "${CYAN}setup${NC}              Database setup and initialization"
    echo "  setup               Full database setup (create + migrate + seed)"
    echo "  create              Create the database"
    echo "  migrate             Run migrations"
    echo "  seed                Seed database with test data"
    echo "  reset               Reset database (drop + create + migrate)"
    echo "  status              Show migration status"
    echo "  drop                Drop the database"
    echo "  postgres            Setup PostgreSQL database"
    echo "  sqlite              Setup SQLite database"
    echo
    echo -e "${CYAN}backup${NC}             Backup and restore operations"
    echo "  backup              Create database backup"
    echo "  restore             Restore database from backup"
    echo "  list                List available backups"
    echo "  clean               Clean old backups"
    echo "  export              Export data to JSON/CSV"
    echo "  import              Import data from JSON/CSV"
    echo "  clone               Clone database to different name"
    echo "  compare             Compare two databases"
    echo
    echo -e "${CYAN}monitor${NC}            Monitoring and health checks"
    echo "  health              Complete health check"
    echo "  status              Quick status check"
    echo "  connections         Show active connections"
    echo "  performance         Show performance metrics"
    echo "  slow-queries        Show slow queries"
    echo "  locks               Show database locks"
    echo "  disk-usage          Show disk usage"
    echo "  memory-usage        Show memory usage"
    echo "  backup-status       Check backup status"
    echo "  monitor             Start continuous monitoring"
    echo "  alerts              Check for alerts"
    echo "  vacuum              Perform database maintenance"
    echo "  analyze             Update database statistics"
    echo "  report              Generate comprehensive report"
    echo
    echo -e "${CYAN}migrate${NC}            Migration management"
    echo "  status              Show migration status"
    echo "  pending             List pending migrations"
    echo "  applied             List applied migrations"
    echo "  run                 Run pending migrations"
    echo "  rollback            Rollback migrations"
    echo "  create              Create new migration"
    echo "  generate            Generate migration from schema diff"
    echo "  validate            Validate migration files"
    echo "  dry-run             Show what would be migrated"
    echo "  force               Force migration state"
    echo "  repair              Repair migration table"
    echo "  baseline            Set migration baseline"
    echo "  history             Show migration history"
    echo "  schema-dump         Dump current schema"
    echo "  data-migrate        Migrate data between schemas"
    echo "  template            Manage migration templates"
    echo
    echo -e "${CYAN}utils${NC}              Database utilities and maintenance"
    echo "  size                Show database size information"
    echo "  tables              List all tables with row counts"
    echo "  indexes             Show index information"
    echo "  constraints         Show table constraints"
    echo "  users               Show database users (PostgreSQL only)"
    echo "  permissions         Show user permissions"
    echo "  sessions            Show active sessions"
    echo "  locks               Show current locks"
    echo "  queries             Show running queries"
    echo "  kill-query          Kill a specific query"
    echo "  optimize            Optimize database (VACUUM, ANALYZE)"
    echo "  reindex             Rebuild indexes"
    echo "  check-integrity     Check database integrity"
    echo "  repair              Repair database issues"
    echo "  cleanup             Clean up temporary data"
    echo "  logs                Show database logs"
    echo "  config              Show database configuration"
    echo "  extensions          List database extensions (PostgreSQL)"
    echo "  sequences           Show sequence information"
    echo "  triggers            Show table triggers"
    echo "  functions           Show user-defined functions"
    echo "  views               Show database views"
    echo "  schema-info         Show comprehensive schema information"
    echo "  duplicate-data      Find duplicate records"
    echo "  orphaned-data       Find orphaned records"
    echo "  table-stats         Show detailed table statistics"
    echo "  connection-test     Test database connection"
    echo "  benchmark           Run database benchmarks"
    echo "  export-schema       Export database schema"
    echo "  import-schema       Import database schema"
    echo "  copy-table          Copy table data"
    echo "  truncate-table      Truncate table data"
    echo "  reset-sequence      Reset sequence values"
    echo
    echo -e "${BOLD}Common Options:${NC}"
    echo "  --env ENV           Environment (dev/prod) [default: dev]"
    echo "  --force             Skip confirmations"
    echo "  --quiet             Suppress verbose output"
    echo "  --debug             Enable debug output"
    echo "  --dry-run           Show what would be done without executing"
    echo "  --help              Show category-specific help"
    echo
    echo -e "${BOLD}Quick Commands:${NC}"
    echo "  $0 status           Quick database status"
    echo "  $0 health           Complete health check"
    echo "  $0 backup           Create backup"
    echo "  $0 migrate          Run migrations"
    echo "  $0 optimize         Optimize database"
    echo
    echo -e "${BOLD}Examples:${NC}"
    echo "  $0 setup create                      # Create database"
    echo "  $0 setup migrate                     # Run migrations"
    echo "  $0 backup create                     # Create backup"
    echo "  $0 backup restore --file backup.sql # Restore from backup"
    echo "  $0 monitor health                    # Health check"
    echo "  $0 monitor connections               # Show connections"
    echo "  $0 migrate create --name add_users   # Create migration"
    echo "  $0 migrate run                       # Run pending migrations"
    echo "  $0 utils size                        # Show database size"
    echo "  $0 utils optimize                    # Optimize database"
    echo
    echo -e "${BOLD}For detailed help on a specific category:${NC}"
    echo "  $0 setup --help"
    echo "  $0 backup --help"
    echo "  $0 monitor --help"
    echo "  $0 migrate --help"
    echo "  $0 utils --help"
}

# Check if required scripts exist
check_scripts() {
    local missing_scripts=()

    if [ ! -f "$SCRIPT_DIR/db-setup.sh" ]; then
        missing_scripts+=("db-setup.sh")
    fi

    if [ ! -f "$SCRIPT_DIR/db-backup.sh" ]; then
        missing_scripts+=("db-backup.sh")
    fi

    if [ ! -f "$SCRIPT_DIR/db-monitor.sh" ]; then
        missing_scripts+=("db-monitor.sh")
    fi

    if [ ! -f "$SCRIPT_DIR/db-migrate.sh" ]; then
        missing_scripts+=("db-migrate.sh")
    fi

    if [ ! -f "$SCRIPT_DIR/db-utils.sh" ]; then
        missing_scripts+=("db-utils.sh")
    fi

    if [ ${#missing_scripts[@]} -gt 0 ]; then
        log_error "Missing required scripts: ${missing_scripts[*]}"
        echo "Please ensure all database management scripts are present in the scripts directory."
        exit 1
    fi
}

# Make scripts executable
make_scripts_executable() {
    chmod +x "$SCRIPT_DIR"/db-*.sh 2>/dev/null || true
}

# Show quick status
show_quick_status() {
    print_header "Quick Database Status"

    # Check if .env exists
    if [ ! -f ".env" ]; then
        log_error ".env file not found"
        echo "Run: $0 setup create"
        return 1
    fi

    # Load environment variables
    export $(grep -v '^#' .env | xargs) 2>/dev/null || true

    # Show basic info
    log "Environment: ${ENVIRONMENT:-dev}"
    log "Database URL: ${DATABASE_URL:-not set}"

    # Test connection
    if command -v "$SCRIPT_DIR/db-utils.sh" >/dev/null 2>&1; then
        "$SCRIPT_DIR/db-utils.sh" connection-test --quiet 2>/dev/null || log_warn "Database connection failed"
    fi

    # Show migration status
    if command -v "$SCRIPT_DIR/db-migrate.sh" >/dev/null 2>&1; then
        "$SCRIPT_DIR/db-migrate.sh" status --quiet 2>/dev/null || log_warn "Could not check migration status"
    fi
}

# Show comprehensive health check
show_health_check() {
    print_header "Comprehensive Database Health Check"

    if [ -f "$SCRIPT_DIR/db-monitor.sh" ]; then
        "$SCRIPT_DIR/db-monitor.sh" health "$@"
    else
        log_error "db-monitor.sh not found"
        exit 1
    fi
}

# Create quick backup
create_quick_backup() {
    print_header "Quick Database Backup"

    if [ -f "$SCRIPT_DIR/db-backup.sh" ]; then
        "$SCRIPT_DIR/db-backup.sh" backup --compress "$@"
    else
        log_error "db-backup.sh not found"
        exit 1
    fi
}

# Run migrations
run_migrations() {
    print_header "Running Database Migrations"

    if [ -f "$SCRIPT_DIR/db-migrate.sh" ]; then
        "$SCRIPT_DIR/db-migrate.sh" run "$@"
    else
        log_error "db-migrate.sh not found"
        exit 1
    fi
}

# Optimize database
optimize_database() {
    print_header "Database Optimization"

    if [ -f "$SCRIPT_DIR/db-utils.sh" ]; then
        "$SCRIPT_DIR/db-utils.sh" optimize "$@"
    else
        log_error "db-utils.sh not found"
        exit 1
    fi
}

# Parse command line arguments
CATEGORY=""
COMMAND=""
REMAINING_ARGS=()

# Handle special single commands
if [[ $# -eq 1 ]]; then
    case $1 in
        "status")
            show_quick_status
            exit 0
            ;;
        "health")
            show_health_check
            exit 0
            ;;
        "backup")
            create_quick_backup
            exit 0
            ;;
        "migrate")
            run_migrations
            exit 0
            ;;
        "optimize")
            optimize_database
            exit 0
            ;;
        "-h"|"--help")
            print_usage
            exit 0
            ;;
    esac
fi

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        -h|--help)
            if [ -n "$CATEGORY" ]; then
                REMAINING_ARGS+=("$1")
            else
                print_usage
                exit 0
            fi
            shift
            ;;
        *)
            if [ -z "$CATEGORY" ]; then
                CATEGORY="$1"
            elif [ -z "$COMMAND" ]; then
                COMMAND="$1"
            else
                REMAINING_ARGS+=("$1")
            fi
            shift
            ;;
    esac
done

# Check if we're in the right directory
if [ ! -f "Cargo.toml" ]; then
    log_error "Please run this script from the project root directory"
    exit 1
fi

# Check that all required scripts exist
check_scripts

# Make scripts executable
make_scripts_executable

# Validate category and command
if [ -z "$CATEGORY" ]; then
    print_usage
    exit 1
fi

# Route to appropriate script
case "$CATEGORY" in
    "setup")
        if [ -z "$COMMAND" ]; then
            log_error "Command required for setup category"
            echo "Use: $0 setup --help for available commands"
            exit 1
        fi
        exec "$SCRIPT_DIR/db-setup.sh" "$COMMAND" "${REMAINING_ARGS[@]}"
        ;;
    "backup")
        if [ -z "$COMMAND" ]; then
            log_error "Command required for backup category"
            echo "Use: $0 backup --help for available commands"
            exit 1
        fi
        exec "$SCRIPT_DIR/db-backup.sh" "$COMMAND" "${REMAINING_ARGS[@]}"
        ;;
    "monitor")
        if [ -z "$COMMAND" ]; then
            log_error "Command required for monitor category"
            echo "Use: $0 monitor --help for available commands"
            exit 1
        fi
        exec "$SCRIPT_DIR/db-monitor.sh" "$COMMAND" "${REMAINING_ARGS[@]}"
        ;;
    "migrate")
        if [ -z "$COMMAND" ]; then
            log_error "Command required for migrate category"
            echo "Use: $0 migrate --help for available commands"
            exit 1
        fi
        exec "$SCRIPT_DIR/db-migrate.sh" "$COMMAND" "${REMAINING_ARGS[@]}"
        ;;
    "utils")
        if [ -z "$COMMAND" ]; then
            log_error "Command required for utils category"
            echo "Use: $0 utils --help for available commands"
            exit 1
        fi
        exec "$SCRIPT_DIR/db-utils.sh" "$COMMAND" "${REMAINING_ARGS[@]}"
        ;;
    *)
        log_error "Unknown category: $CATEGORY"
        echo
        print_usage
        exit 1
        ;;
esac
