#!/bin/bash
set -e

MIGRATION_TYPE="$1"

if [ -z "$MIGRATION_TYPE" ]; then
    echo "Error: No migration type specified"
    echo ""
    echo "Usage: $0 <all|persons|cyclotron|behavioral-cohorts>"
    echo "  all                - Run all migrations"
    echo "  persons            - Run only persons migrations"
    echo "  cyclotron          - Run only cyclotron migrations"
    echo "  behavioral-cohorts - Run only behavioral cohorts migrations"
    exit 1
fi

# Determine the base directory for migrations
# In Docker, we use /migrations; locally, we use the parent directory
if [ -d "/migrations" ]; then
    MIGRATIONS_BASE="/migrations"
else
    SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
    MIGRATIONS_BASE="$SCRIPT_DIR/.."
fi

run_persons_migrations() {
    echo "Running persons migrations..."

    PERSONS_DATABASE_NAME=${PERSONS_DATABASE_NAME:-posthog_persons}
    PERSONS_DATABASE_URL=${PERSONS_DATABASE_URL:-postgres://posthog:posthog@localhost:5432/$PERSONS_DATABASE_NAME}

    echo "Performing persons migrations for $PERSONS_DATABASE_URL"

    sqlx database create -D "$PERSONS_DATABASE_URL"

    sqlx migrate run -D "$PERSONS_DATABASE_URL" --source "$MIGRATIONS_BASE/persons_migrations/"

    echo "Persons migrations completed successfully"
}

run_cyclotron_migrations() {
    echo "Running cyclotron migrations..."

    CYCLOTRON_DATABASE_NAME=${CYCLOTRON_DATABASE_NAME:-cyclotron}
    CYCLOTRON_DATABASE_URL=${CYCLOTRON_DATABASE_URL:-postgres://posthog:posthog@localhost:5432/$CYCLOTRON_DATABASE_NAME}

    echo "Performing cyclotron migrations for $CYCLOTRON_DATABASE_URL"

    sqlx database create -D "$CYCLOTRON_DATABASE_URL"

    sqlx migrate run -D "$CYCLOTRON_DATABASE_URL" --source "$MIGRATIONS_BASE/cyclotron-core/migrations/"

    echo "Cyclotron migrations completed successfully"
}

run_behavioral_cohorts_migrations() {
    echo "Running behavioral cohorts migrations..."

    BEHAVIORAL_COHORTS_DATABASE_NAME=${BEHAVIORAL_COHORTS_DATABASE_NAME:-behavioral_cohorts}

    # Use environment variables with defaults
    DB_HOST="${POSTGRES_BEHAVIORAL_COHORTS_HOST:-localhost}"
    DB_USER="${POSTGRES_BEHAVIORAL_COHORTS_USER:-posthog}"
    DB_PASS="${POSTGRES_BEHAVIORAL_COHORTS_PASSWORD:-posthog}"
    
    BEHAVIORAL_COHORTS_DATABASE_URL=${BEHAVIORAL_COHORTS_DATABASE_URL:-postgres://${DB_USER}:${DB_PASS}@${DB_HOST}:5432/$BEHAVIORAL_COHORTS_DATABASE_NAME}

    echo "Performing behavioral cohorts migrations for $BEHAVIORAL_COHORTS_DATABASE_URL"
    echo "Database name: ${BEHAVIORAL_COHORTS_DATABASE_NAME}"

    sqlx database create -D "$BEHAVIORAL_COHORTS_DATABASE_URL"

    sqlx migrate run -D "$BEHAVIORAL_COHORTS_DATABASE_URL" --source "$MIGRATIONS_BASE/behavioral_cohorts_migrations/"

    echo "Behavioral cohorts migrations completed successfully"
}

case "$MIGRATION_TYPE" in
    persons)
        run_persons_migrations
        ;;
    cyclotron)
        run_cyclotron_migrations
        ;;
    behavioral-cohorts)
        run_behavioral_cohorts_migrations
        ;;
    all)
        run_persons_migrations
        run_cyclotron_migrations
        run_behavioral_cohorts_migrations
        ;;
    *)
        echo "Usage: $0 <all|persons|cyclotron|behavioral-cohorts>"
        echo "  all                - Run all migrations"
        echo "  persons            - Run only persons migrations"
        echo "  cyclotron          - Run only cyclotron migrations"
        echo "  behavioral-cohorts - Run only behavioral cohorts migrations"
        exit 1
        ;;
esac

echo "All requested migrations completed successfully"
