#!/usr/bin/env bash
set -euo pipefail

usage() {
    cat <<'EOF'
Usage:
  run_billing_readiness.sh \
    --app-root /path/to/staging/app \
    --run-dir /path/to/evidence/run-YYYYMMDD-HHMMSS \
    --db-defaults-file /path/to/.my.cnf \
    --db-name database \
    --confirm READ_ONLY_PRODUCTION \
    [--sync --from YYYY-MM-DD --to YYYY-MM-DD]

This sequence is intended for STAGING with an isolated database. It authenticates
against FACTO production, performs GET requests, and optionally imports production
metadata/documents into the staging database. It never issues a DTE.
EOF
}

APP_ROOT=""
RUN_DIR=""
DB_DEFAULTS_FILE=""
DB_NAME=""
CONFIRM=""
SYNC=0
FROM_DATE=""
TO_DATE=""
PHP_BIN="php"

while [ "$#" -gt 0 ]; do
    case "$1" in
        --app-root)
            APP_ROOT=${2:-}
            shift 2
            ;;
        --run-dir)
            RUN_DIR=${2:-}
            shift 2
            ;;
        --db-defaults-file)
            DB_DEFAULTS_FILE=${2:-}
            shift 2
            ;;
        --db-name)
            DB_NAME=${2:-}
            shift 2
            ;;
        --confirm)
            CONFIRM=${2:-}
            shift 2
            ;;
        --sync)
            SYNC=1
            shift
            ;;
        --from)
            FROM_DATE=${2:-}
            shift 2
            ;;
        --to)
            TO_DATE=${2:-}
            shift 2
            ;;
        --php-bin)
            PHP_BIN=${2:-}
            shift 2
            ;;
        --help|-h)
            usage
            exit 0
            ;;
        *)
            echo "Unknown argument: $1" >&2
            usage >&2
            exit 1
            ;;
    esac
done

if [ -z "$APP_ROOT" ] || [ -z "$RUN_DIR" ] || [ -z "$DB_DEFAULTS_FILE" ] || [ -z "$DB_NAME" ] || [ "$CONFIRM" != "READ_ONLY_PRODUCTION" ]; then
    usage >&2
    exit 1
fi

APP_ROOT=$(cd "$APP_ROOT" 2>/dev/null && pwd) || {
    echo "Application root does not exist." >&2
    exit 1
}
if [ ! -f "$DB_DEFAULTS_FILE" ]; then
    echo "Database defaults file was not found." >&2
    exit 1
fi
if [ "$SYNC" -eq 1 ]; then
    if ! printf '%s' "$FROM_DATE" | grep -Eq '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' || ! printf '%s' "$TO_DATE" | grep -Eq '^[0-9]{4}-[0-9]{2}-[0-9]{2}$'; then
        echo "--from and --to in YYYY-MM-DD format are required with --sync." >&2
        exit 1
    fi
fi

mkdir -p "$RUN_DIR"
RUN_DIR=$(cd "$RUN_DIR" && pwd)
chmod 0700 "$RUN_DIR" 2>/dev/null || true

# The SQL report contains operational metadata. Keep it private.
"$PHP_BIN" "$APP_ROOT/scripts/billing_production_preflight.php" \
    --app-root="$APP_ROOT" \
    --env=production \
    --stage=readiness \
    --strict \
    --fix-dirs \
    --json="$RUN_DIR/preflight-before.json" \
    | tee "$RUN_DIR/preflight-before.txt"

mysql \
    --defaults-extra-file="$DB_DEFAULTS_FILE" \
    "$DB_NAME" \
    < "$APP_ROOT/database/preflight/2026_07_13_billing_production_readiness.sql" \
    > "$RUN_DIR/database-preflight-before.txt"
chmod 0600 "$RUN_DIR/database-preflight-before.txt" 2>/dev/null || true

"$PHP_BIN" "$APP_ROOT/scripts/facto_production_probe.php" \
    --app-root="$APP_ROOT" \
    --env=production \
    --scope=metadata \
    --confirm=READ_ONLY_PRODUCTION \
    --json="$RUN_DIR/facto-metadata-probe.json" \
    | tee "$RUN_DIR/facto-metadata-probe.txt"

if [ "$SYNC" -eq 1 ]; then
    "$PHP_BIN" "$APP_ROOT/cron/sync_facto_customers.php" \
        --env=production \
        --per-page=100 \
        --force=false \
        | tee "$RUN_DIR/sync-customers.txt"

    "$PHP_BIN" "$APP_ROOT/cron/sync_facto_documents.php" \
        --env=production \
        --from="$FROM_DATE" \
        --to="$TO_DATE" \
        --per-page=100 \
        --fetch-files=false \
        | tee "$RUN_DIR/sync-issued-documents.txt"

    "$PHP_BIN" "$APP_ROOT/cron/sync_facto_inbox_documents.php" \
        --env=production \
        --per-page=100 \
        --fetch-files=false \
        | tee "$RUN_DIR/sync-inbox-documents.txt"

    "$PHP_BIN" "$APP_ROOT/scripts/facto_production_probe.php" \
        --app-root="$APP_ROOT" \
        --env=production \
        --scope=full \
        --confirm=READ_ONLY_PRODUCTION \
        --json="$RUN_DIR/facto-full-probe.json" \
        | tee "$RUN_DIR/facto-full-probe.txt"
fi

"$PHP_BIN" "$APP_ROOT/scripts/billing_production_preflight.php" \
    --app-root="$APP_ROOT" \
    --env=production \
    --stage=readiness \
    --strict \
    --json="$RUN_DIR/preflight-after.json" \
    | tee "$RUN_DIR/preflight-after.txt"

mysql \
    --defaults-extra-file="$DB_DEFAULTS_FILE" \
    "$DB_NAME" \
    < "$APP_ROOT/database/preflight/2026_07_13_billing_production_readiness.sql" \
    > "$RUN_DIR/database-preflight-after.txt"
chmod 0600 "$RUN_DIR/database-preflight-after.txt" 2>/dev/null || true

cat <<EOF
Read-only production readiness sequence completed.
Evidence directory: $RUN_DIR
No DTE issuance endpoint was called by this script.
EOF
