#!/usr/bin/env bash

REPORT_UI_ONLY="${REPORT_UI_ONLY:-problems}"
REPORT_UI_DETAILS=false
REPORT_UI_JSON=false
# shellcheck disable=SC2034
REPORT_UI_VERBOSE=false
REPORT_UI_ROWS_FILE=""

report_ui_usage_flags() {
    cat <<'EOF'
Report UI flags:
  --all             Show ok, skip, warning, and failing items.
  --details         Show the compact report plus detail fields.
  --json            Print structured JSON.
  --verbose         Use the older full diagnostic output when supported.
EOF
}

report_ui_parse_args() {
    REPORT_UI_ONLY="${REPORT_UI_ONLY:-problems}"
    REPORT_UI_DETAILS=false
    REPORT_UI_JSON=false
    REPORT_UI_VERBOSE=false
    while (($# > 0)); do
        case "$1" in
            --all)
                REPORT_UI_ONLY="all"
                shift
                ;;
            --details)
                REPORT_UI_DETAILS=true
                shift
                ;;
            --json)
                REPORT_UI_JSON=true
                shift
                ;;
            --verbose)
                # shellcheck disable=SC2034
                REPORT_UI_VERBOSE=true
                shift
                ;;
            -h|--help)
                report_ui_usage_flags
                return 2
                ;;
            *)
                echo "unknown report option: $1" >&2
                return 1
                ;;
        esac
    done
}

report_ui_begin() {
    REPORT_UI_ROWS_FILE="$(mktemp "${TMPDIR:-/tmp}/jeannie-report-rows.XXXXXX")"
}

report_ui_cleanup() {
    if [[ -n "${REPORT_UI_ROWS_FILE:-}" && -f "${REPORT_UI_ROWS_FILE}" ]]; then
        rm -f "${REPORT_UI_ROWS_FILE}"
    fi
    REPORT_UI_ROWS_FILE=""
}

report_ui_one_line() {
    local value="${1:-}"
    value="${value//$'\t'/ }"
    value="${value//$'\r'/ }"
    value="${value//$'\n'/ | }"
    printf '%s' "${value}"
}

report_ui_first_line() {
    awk 'NF && $0 !~ /^\+/ { print; exit }'
}

report_ui_trim_detail() {
    sed -n '1,20p'
}

report_ui_emit() {
    local status="$1"
    local area="$2"
    local check="$3"
    local summary="$4"
    local detail="${5:-}"
    local fix="${6:-}"
    local graph="${7:-}"
    local query="${8:-}"

    if [[ -z "${REPORT_UI_ROWS_FILE:-}" ]]; then
        echo "report_ui_begin was not called" >&2
        return 1
    fi

    printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' \
        "$(report_ui_one_line "${status}")" \
        "$(report_ui_one_line "${area}")" \
        "$(report_ui_one_line "${check}")" \
        "$(report_ui_one_line "${summary}")" \
        "$(report_ui_one_line "${detail}")" \
        "$(report_ui_one_line "${fix}")" \
        "$(report_ui_one_line "${graph}")" \
        "$(report_ui_one_line "${query}")" >>"${REPORT_UI_ROWS_FILE}"
}

report_ui_check() {
    local severity="$1"
    local area="$2"
    local check="$3"
    local fix="${4:-}"
    local graph="${5:-}"
    local query="${6:-}"
    shift 6
    local output
    local status
    local summary
    local detail

    set +e
    output="$("$@" 2>&1)"
    status=$?
    set -e

    if [[ "${status}" -eq 0 ]]; then
        report_ui_emit ok "${area}" "${check}" "ok" "${output}"
        return 0
    fi

    summary="$(printf '%s\n' "${output}" | report_ui_first_line)"
    if [[ -z "${summary}" ]]; then
        summary="check exited with status ${status}"
    fi
    detail="$(printf '%s\n' "${output}" | report_ui_trim_detail)"
    if [[ "${severity}" == "fail" ]]; then
        report_ui_emit fail "${area}" "${check}" "${summary}" "${detail}" "${fix}" "${graph}" "${query}"
        return 1
    fi
    report_ui_emit warn "${area}" "${check}" "${summary}" "${detail}" "${fix}" "${graph}" "${query}"
    return 0
}

report_ui_render() {
    local title="$1"
    local -a args=(--title "${title}" --only "${REPORT_UI_ONLY}")

    if [[ "${REPORT_UI_DETAILS}" == "true" ]]; then
        args+=(--details)
    fi
    if [[ "${REPORT_UI_JSON}" == "true" ]]; then
        args+=(--json)
    fi

    "${REPO_ROOT}/scripts/report-render" "${args[@]}" <"${REPORT_UI_ROWS_FILE}"
}
