#!/usr/bin/env bash
# shellcheck shell=bash

[ ! -z "$DEBUG" ] && set -x

# Default values
PSQL_VERSION="15"
MIGRATION_FILE=""
PORTNO="@PGSQL_DEFAULT_PORT@"
PSQL_USER="postgres"

# Function to display help
print_help() {
    echo "Usage: nix run .#start-client -- [options]"
    echo
    echo "Options:"
    echo "  -v, --version [15|16|orioledb-16]  Specify the PostgreSQL version to use (required)"
    echo "  -f, --file FILE                    Provide a custom migration script"
    echo "  -u, --user USER                    Specify the user/role to use (default: postgres)"
    echo "  -h, --help                         Show this help message"
    echo
    echo "Description:"
    echo "  Starts an interactive 'psql' session connecting to a Postgres database started with the"
    echo "  'nix run .#start-server' command. If a migration file is not provided, the client"
    echo "  initializes the database with the default migrations for a new Supabase project."
    echo "  If a migrations file is provided, default migrations are skipped"
    echo "  If no migration file is provided, it runs the default Supabase migrations."
    echo
    echo "Examples:"
    echo "  nix run .#start-client"
    echo "  nix run .#start-client -- --version 15"
    echo "  nix run .#start-client -- --version 16 --file custom_migration.sql"
    echo "  nix run .#start-client -- --version 16 --port 5433"
    echo "  nix run .#start-client -- --version 16 --user supabase_admin"
}

# Parse arguments
while [[ "$#" -gt 0 ]]; do
    case "$1" in
        -v|--version)
            if [[ -n "$2" && ! "$2" =~ ^- ]]; then
                PSQL_VERSION="$2"
                shift 2
            else
                echo "Error: --version requires an argument (15, 16, or orioledb-16)"
                exit 1
            fi
            ;;
        -f|--file)
            if [[ -n "$2" && ! "$2" =~ ^- ]]; then
                MIGRATION_FILE="$2"
                shift 2
            else
                echo "Error: --file requires a filename"
                exit 1
            fi
            ;;
        -u|--user)
            if [[ -n "$2" && ! "$2" =~ ^- ]]; then
                PSQL_USER="$2"
                shift 2
            else
                echo "Error: --user requires an argument"
                exit 1
            fi
            ;;
        -p|--port)
            if [[ -n "$2" && ! "$2" =~ ^- ]]; then
                PORTNO="$2"
                shift 2
            else
                echo "Error: --port requires an argument"
                exit 1
            fi
            ;;
        -h|--help)
            print_help
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            print_help
            exit 1
            ;;
    esac
done

# Check if version is provided
if [[ -z "$PSQL_VERSION" ]]; then
    echo "Error: PostgreSQL version is required."
    print_help
    exit 1
fi

# Determine PostgreSQL version
if [ "$PSQL_VERSION" == "15" ]; then
    echo "Starting client for PSQL 15"
    PSQL15=@PSQL15_BINDIR@
    BINDIR="$PSQL15"
elif [ "$PSQL_VERSION" == "16" ]; then
    echo "Starting client for PSQL 16"
    PSQL16=@PSQL16_BINDIR@
    BINDIR="$PSQL16"
elif [ "$PSQL_VERSION" == "orioledb-17" ]; then
    echo "Starting client for PSQL ORIOLEDB 17"
    PSQLORIOLEDB16=@PSQLORIOLEDB17_BINDIR@
    BINDIR="$PSQLORIOLEDB16"
else
    echo "Please provide a valid Postgres version (15, 16, or orioledb-16)"
    exit 1
fi

#vars for migration.sh
export PATH=$BINDIR/bin:$PATH
export POSTGRES_DB=postgres
export POSTGRES_HOST=localhost

PGSQL_SUPERUSER=@PGSQL_SUPERUSER@
MIGRATIONS_DIR=@MIGRATIONS_DIR@
POSTGRESQL_SCHEMA_SQL=@POSTGRESQL_SCHEMA_SQL@
PGBOUNCER_AUTH_SCHEMA_SQL=@PGBOUNCER_AUTH_SCHEMA_SQL@
STAT_EXTENSION_SQL=@STAT_EXTENSION_SQL@

# Start interactive psql session
exec psql -U "$PSQL_USER" -p "$PORTNO" -h localhost postgres
