#!/bin/bash
set -euo pipefail

APP_DIR="${1:-}"
RESET_DB="${2:-}"

if [ -z "$APP_DIR" ]; then
    echo "Usage: validate <app_directory> [--reset]"
    echo "  --reset  drop and recreate the database (dev only, DESTROYS all data)"
    exit 1
fi

if [ ! -d "$APP_DIR" ]; then
    echo "Error: Directory '$APP_DIR' does not exist"
    exit 1
fi

cd "$APP_DIR"

# load .env if present
if [ -f .env ]; then
    set -a
    source .env
    set +a
fi

# share cargo build cache across all apps
export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-/tmp/webapp-cargo-cache}"

# run migrations FIRST - compile-time sqlx macros need schema to exist
echo "=== database migration ==="
if [ -z "${DATABASE_URL:-}" ]; then
    echo "SKIP (DATABASE_URL not set - run ./scripts/neon-setup)"
elif [ ! -d migrations ] || [ -z "$(ls -A migrations 2>/dev/null)" ]; then
    echo "SKIP (no migrations found)"
elif ! command -v sqlx &> /dev/null; then
    echo "ERROR: sqlx-cli required for migrations"
    echo "Install: cargo install sqlx-cli --features postgres,native-tls"
    exit 1
elif [ "$RESET_DB" = "--reset" ]; then
    echo "Resetting database (--reset)..."
    sqlx database reset -y --source ./migrations
    echo "PASS"
else
    if ! sqlx migrate run --source ./migrations; then
        echo "ERROR: migration failed"
        echo "If this is a checksum mismatch, a previously applied migration was edited."
        echo "Fix: revert the edit and add a NEW migration file instead."
        echo "Last resort (dev only, DESTROYS all data): validate $APP_DIR --reset"
        exit 1
    fi
    echo "PASS"
fi

echo "=== cargo check ==="
cargo check
echo "PASS"

echo "=== cargo clippy (warnings = errors) ==="
cargo clippy -- -D warnings
echo "PASS"

echo "=== cargo test ==="
cargo test
echo "PASS"

echo "=== cargo build --release ==="
cargo build --release
echo "PASS"

echo "=== sqlx prepare (offline mode) ==="
if [ -z "${DATABASE_URL:-}" ]; then
    echo "SKIP (DATABASE_URL not set)"
elif command -v sqlx &> /dev/null; then
    cargo sqlx prepare
    echo "PASS"
else
    echo "SKIP (sqlx-cli not installed)"
fi

echo "=== sqlx offline mode check ==="
# check if Dockerfile uses SQLX_OFFLINE=true
if [ -f Dockerfile ] && grep -q "SQLX_OFFLINE=true" Dockerfile; then
    # SQLX_OFFLINE is used - require .sqlx directory with content
    if [ ! -d .sqlx ]; then
        echo "ERROR: Dockerfile uses SQLX_OFFLINE=true but .sqlx directory doesn't exist"
        echo "Run 'cargo sqlx prepare' with DATABASE_URL set to generate offline query data"
        exit 1
    fi
    if [ -z "$(ls -A .sqlx 2>/dev/null)" ]; then
        echo "ERROR: Dockerfile uses SQLX_OFFLINE=true but .sqlx directory is empty"
        echo "Run 'cargo sqlx prepare' with DATABASE_URL set to generate offline query data"
        exit 1
    fi
    echo "PASS (SQLX_OFFLINE enabled with prepared queries)"
else
    # SQLX_OFFLINE not used - runtime queries are fine
    echo "PASS (runtime queries - SQLX_OFFLINE not used)"
fi

echo "=== Dockerfile runtime dependencies ==="
if [ ! -d templates ]; then
    echo "ERROR: templates/ directory not found - Askama needs templates at runtime"
    echo "Create templates/ directory with .html files"
    exit 1
fi
if grep -q "COPY templates" Dockerfile 2>/dev/null; then
    echo "PASS (templates/ copied in Dockerfile)"
else
    echo "ERROR: Dockerfile missing 'COPY templates ./templates' - app will fail at runtime"
    echo "Add 'COPY templates ./templates' to Dockerfile before CMD"
    exit 1
fi

echo "=== All validations passed ==="
