#!/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

# Ensure table/column names with spaces or special chars are handled (using quotes in SQL)
# Basic quoting - adjust if table/col names can contain double quotes themselves
SAFE_TABLE="${TABLE//\"/\\\"}"
SAFE_COL="${COL//\"/\\\"}"

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

# Prepare the command arguments (no eval needed now)
CMD_ARRAY=()
POST_PROCESS_PIPE=""

if [[ "$MODE" == "distinct_count" ]]; then
    CMD_ARRAY=(qdb-cli exec --psql -q "$SQL")
else
    CMD_ARRAY=(qdb-cli exp "$SQL")
    # Use a function for post-processing to avoid complex quoting with pipes
    post_process() {
        tail -n +2 | tr -d '"' | tr -d '\r'
    }
fi

# Construct the string representation for dry-run/verbose output
CMD_STR="${CMD_ARRAY[*]}"
if [[ -n "$POST_PROCESS_PIPE" ]] || [[ "$MODE" != "distinct_count" ]]; then
    CMD_STR+=" | tail -n +2 | tr -d '\"' | tr -d '\\r'"
fi

# Dry run?
if [[ $DRY_RUN -eq 1 ]]; then
    # Use printf for safer output of the command string
    printf "Dry run: %s\n" "$CMD_STR"
    exit 0
fi

# Verbose?
if [[ $VERBOSE -eq 1 ]]; then
    printf "Running: %s\n" "$CMD_STR"
fi

# Execute
if [[ "$MODE" == "distinct_count" ]]; then
    # Execute directly
    "${CMD_ARRAY[@]}"
else
    # Execute and pipe to post-processing function
    "${CMD_ARRAY[@]}" | post_process
fi
