# List this menu and exit
help:
    just --justfile "{{ justfile_directory() }}/database/justfile" --list

compose_file := justfile_directory() / "database" / "docker-compose.yml"

# Open rainfrog PostgreSQL TUI client
tui:
    #!/usr/bin/env amber
    main {
        let env_file = "{{ justfile_directory() }}/.env.development"
        let pg_host = $ dotenvx get PGHOST -f {env_file} $?
        let pg_port = $ dotenvx get PGPORT -f {env_file} $?
        let pg_user = $ dotenvx get PGUSER -f {env_file} $?
        let pg_pass = $ dotenvx get PGPASSWORD -f {env_file} $?
        let pg_db = $ dotenvx get PGDATABASE -f {env_file} $?

        $ rainfrog --driver postgres --username {pg_user} --password {pg_pass} --host {pg_host} --port {pg_port} --database {pg_db}$?
    }

# Start PostgreSQL container
start:
    #!/usr/bin/env amber
    main {
        let compose_file = "{{ compose_file }}"
        let env_file = "{{ justfile_directory() }}/.env.development"

        let project = $ dotenvx get PROJECT -f {env_file} $?
        let pg_user = $ dotenvx get PGUSER -f {env_file} $?
        let pg_port = $ dotenvx get PGPORT -f {env_file} $?

        cd "{{ justfile_directory() }}"
        $ dotenvx run -f {env_file} -- podman compose -f {compose_file} up --build --force-recreate -d $?

        // Wait for readiness (check inside the container)
        let retries = 0
        loop {
            if retries >= 30 {
                echo "PostgreSQL did not become ready in time."
                fail 1
            }
            trust $ podman exec {project}-postgres pg_isready -U {pg_user} $
            if status == 0 {
                break
            }
            $ sleep 0.2 $?
            retries += 1
        }

        echo "PostgreSQL container started on port {pg_port}."
    }

# Stop PostgreSQL container
stop:
    #!/usr/bin/env amber
    main {
        let compose_file = "{{ compose_file }}"
        let env_file = "{{ justfile_directory() }}/.env.development"
        cd "{{ justfile_directory() }}"
        $ dotenvx run -f {env_file} -- podman compose -f {compose_file} stop postgres $?
        exit 0
    }

# Stop PostgreSQL container and remove data volume
destroy:
    #!/usr/bin/env amber
    main {
        let compose_file = "{{ compose_file }}"
        let env_file = "{{ justfile_directory() }}/.env.development"
        cd "{{ justfile_directory() }}"
        $ dotenvx run -f {env_file} -- podman compose -f {compose_file} down -v $?
        exit 0
    }

# Create a migration for the database
new_migration *description:
    #!/usr/bin/env amber
    import { parse_int, zfill } from "std/text"
    main {
        let deploy_dir = "{{ justfile_directory() }}/database/deploy"
        let old_id = $ ls -1 {deploy_dir}/*.sql | sed 's|.*/||' | sed 's|\.sql||' | sort -n | tail -1 $?
        let old_id_num = parse_int(old_id)?
        let new_id = zfill((old_id_num + 1) as Text, 8)

        cd "{{ justfile_directory() }}"
        $ sqitch add {new_id} --requires {old_id} -n '{{ description }}' $?
        exit 0
    }

# Migrate the database to the most recent revision
migrate:
    #!/usr/bin/env amber
    main {
        let env_file = "{{ justfile_directory() }}/.env.development"
        cd "{{ justfile_directory() }}"
        $ dotenvx run -f {env_file} -- sqitch deploy $?
        exit 0
    }

# Migrate the database to a specific revision
upgrade_to rev:
    #!/usr/bin/env amber
    main {
        let env_file = "{{ justfile_directory() }}/.env.development"
        cd "{{ justfile_directory() }}"
        $ dotenvx run -f {env_file} -- sqitch deploy --to "{{ rev }}" $?
        exit 0
    }

# Revert the database to the initial state
revert:
    #!/usr/bin/env amber
    main {
        let env_file = "{{ justfile_directory() }}/.env.development"
        cd "{{ justfile_directory() }}"
        $ dotenvx run -f {env_file} -- sqitch revert $?
        exit 0
    }

# Revert the database to a specific revision
revert_to rev:
    #!/usr/bin/env amber
    main {
        let env_file = "{{ justfile_directory() }}/.env.development"
        cd "{{ justfile_directory() }}"
        $ dotenvx run -f {env_file} -- sqitch revert --to "{{ rev }}" $?
        exit 0
    }
