#!/usr/bin/env bash

show_help() {
    cat <<EOF
Usage: $(basename "$0") [OPTIONS] <table_name> [<col_name>]
Run a canned QuestDB query and clean up the output.

Modes (mutually exclusive):
  (default)       Count mode:
                  SELECT count() FROM "<table_name>";
                  → uses qdb-cli exp and strips header/quotes
  -d, --distinct  Distinct mode:
                  SELECT distinct ("<col_name>") FROM "<table_name>";
                  → uses qdb-cli exp and strips header/quotes
  -c, --count     Distinct count mode:
                  SELECT distinct ("<col_name>"), count() FROM "<table_name>";
                  → uses qdb-cli exec --psql (no post‐processing)

Options:
  -n, --dry-run   Show the command that would be run, but do not execute it.
  -v, --verbose   Show the command before running it.
  -h, --help      Show this help message and exit.

Examples:
  $(basename "$0") cme_liq_ba_ES
  $(basename "$0") -d cme_liq_ba_ES CT
  $(basename "$0") -c cme_liq_ba_ES CT
  $(basename "$0") -n -c cme_liq_ba_ES CT
EOF
}

# Defaults
MODE="count"
DRY_RUN=0
VERBOSE=0

# Parse flags
while [[ $# -gt 0 ]]; do
    case "$1" in
    -d | --distinct)
        MODE="distinct"
        shift
        ;;
    -c | --count)
        MODE="distinct_count"
        shift
        ;;
    -n | --dry-run)
        DRY_RUN=1
        shift
        ;;
    -v | --verbose)
        VERBOSE=1
        shift
        ;;
    -h | --help)
        show_help
        exit 0
        ;;
    --)
        shift
        break
        ;;
    -*)
        echo "Unknown option: $1" >&2
        show_help
        exit 1
        ;;
    *) break ;;
    esac
done

# Positional args
TABLE="$1"
COL="$2"

if [[ -z "$TABLE" ]]; then
    echo "Error: <table_name> is required." >&2
    show_help
    exit 1
fi

# Build the SQL
case "$MODE" in
count)
    SQL="select count() from \"${TABLE}\";"
    ;;
distinct)
    if [[ -z "$COL" ]]; then
        echo "Error: <col_name> is required for distinct mode." >&2
        show_help
        exit 1
    fi
    SQL="select distinct (\"${COL}\") from \"${TABLE}\";"
    ;;
distinct_count)
    if [[ -z "$COL" ]]; then
        echo "Error: <col_name> is required for distinct count mode." >&2
        show_help
        exit 1
    fi
    SQL="select distinct (\"${COL}\"), count() from \"${TABLE}\";"
    ;;
esac

# Prepare the command
if [[ "$MODE" == "distinct_count" ]]; then
    CMD_STR="qdb-cli exec --psql -q \"${SQL}\""
else
    CMD_STR="qdb-cli exp \"${SQL}\" | tail -n +2 | tr -d '\"' | tr -d '\r'"
fi

# Dry run?
if [[ $DRY_RUN -eq 1 ]]; then
    echo "Dry run: $CMD_STR"
    exit 0
fi

# Verbose?
if [[ $VERBOSE -eq 1 ]]; then
    echo "Running: $CMD_STR"
fi

# Execute
if [[ "$MODE" == "distinct_count" ]]; then
    eval qdb-cli exec --psql -q "'${SQL}'"
else
    eval qdb-cli exp "'${SQL}'" \| tail -n +2 \| tr -d '"' \| tr -d '\r'
fi
