# shellcheck shell=dash

# Section: website statistics active
# https://umami.is/docs/api/website-stats#get-/api/websites/:websiteid/active
___x_cmd_umami_site_active(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift
    case "$op" in
        --app)        ___x_cmd_umami_site_active___app   "$@" ;;
        --raw)        ___x_cmd_umami_site_active___raw   "$@" ;;
        --json)       ___x_cmd_umami_site_active___json  "$@" ;;
        -h|--help)    ___x_cmd help -m umami site active        "$@" ;;
        *)            ___x_cmd_umami_site_active___raw   "$op" "$@" ;;
    esac
}
___x_cmd_umami_site_active___raw(){
    local _website_id="$1";  [ -n "$_website_id" ] || M='Provide a websiteID'   N=umami log:ret:1
    ___x_cmd_umami___transform_website_id || return

    ___x_cmd_umami_api get "$___X_CMD_UMAMI___API_ENDPOINT/${_website_id}/active"
}

___x_cmd_umami_site_active___json(){
    ___x_cmd_umami_site_active___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_umami_site_active___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_umami_site_active___raw "$@" | ___x_cmd jo fmt | ___x_cmd csv app
    else
        ___x_cmd_umami_site_active___raw "$@" | ___x_cmd jo fmt | ___x_cmd csv tsv
    fi
}
# EndSection

# Section: website statistics events
# https://umami.is/docs/api/website-stats#get-/api/websites/:websiteid/events
___x_cmd_umami_site_event(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift
    case "$op" in
        --app)        ___x_cmd_umami_site_event___app   "$@" ;;
        --raw)        ___x_cmd_umami_site_event___raw   "$@" ;;
        --json)       ___x_cmd_umami_site_event___json  "$@" ;;
        -h|--help)    ___x_cmd help -m umami site event        "$@" ;;
        *)            ___x_cmd_umami_site_event___raw   "$op" "$@" ;;
    esac
}
___x_cmd_umami_site_event___raw(){
    local arg=;local key=;
    local _start=; local _end=; local _unit=; local _tz=
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --startAt)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _start=1
                shift 2;;
            --endAt)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _end=1
                shift 2;;
            --unit)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _unit=1
                shift 2;;
            --timezone)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _tz=1
                shift 2;;
            *)
                break ;;
        esac
    done
    [ -n "$_start" ] || arg="startAt=$(___x_cmd_umami___date -15)&${arg}"
    [ -n "$_end" ]   || arg="endAt=$(___x_cmd_umami___date)&${arg}"
    [ -n "$_tz" ]    || arg="${arg}&timezone=Asia/Shanghai"
    [ -n "$_unit" ]  || arg="?${arg}&unit=hour"

    local _website_id="$1";  [ -n "$_website_id" ] || M='Provide a websiteID'   N=umami log:ret:1
    ___x_cmd_umami___transform_website_id || return

    ___x_cmd_umami_api get "$___X_CMD_UMAMI___API_ENDPOINT/${_website_id}/events${arg}"
}

___x_cmd_umami_site_event___json(){
    ___x_cmd_umami_site_event___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_umami_site_event___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_umami_site_event___raw "$@" | ___x_cmd jo fmt | ___x_cmd csv app
    else
        ___x_cmd_umami_site_event___raw "$@" | ___x_cmd jo fmt | ___x_cmd csv tsv
    fi
}
# EndSection

# Section: website statistics pageviews
# https://umami.is/docs/api/website-stats#get-/api/websites/:websiteid/pageviews
___x_cmd_umami_site_pv(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift
    case "$op" in
        --app)        ___x_cmd_umami_site_pv___app   "$@" ;;
        --raw)        ___x_cmd_umami_site_pv___raw   "$@" ;;
        --json)       ___x_cmd_umami_site_pv___json  "$@" ;;
        -h|--help)    ___x_cmd help -m umami site pv        "$@" ;;
        *)            ___x_cmd_umami_site_pv___raw   "$op" "$@" ;;
    esac
}
___x_cmd_umami_site_pv___raw(){
    local arg=;local key=;
    local _start=; local _end=; local _tz=;
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --startAt)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _start=1
                shift 2;;
            --endAt)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _end=1
                shift 2;;
            --unit)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _unit=1
                shift 2;;
            --timezone|--referrer|--title|--host\
            |--os|--brower|--device|--country|--region|--city)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _tz=1
                shift 2;;
            *)
                break ;;
        esac
    done
    [ -n "$_start" ] || arg="startAt=$(___x_cmd_umami___date -15)&${arg}"
    [ -n "$_end" ]   || arg="endAt=$(___x_cmd_umami___date)&${arg}"
    [ -n "$_tz" ]     || arg="${arg}&timezone=Asia/Shanghai"
    [ -n "$_unit" ]  || arg="?${arg}&unit=hour"

    local _website_id="$1";  [ -n "$_website_id" ] || M='Provide a websiteID'   N=umami log:ret:1
    ___x_cmd_umami___transform_website_id || return

    ___x_cmd_umami_api get "$___X_CMD_UMAMI___API_ENDPOINT/${_website_id}/pageviews${arg}"
}

___x_cmd_umami_site_pv___json(){
    ___x_cmd_umami_site_pv___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_umami_site_pv___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_umami_site_pv___raw "$@" | ___x_cmd jo fmt | ___x_cmd csv app
    else
        ___x_cmd_umami_site_pv___raw "$@" | ___x_cmd jo fmt | ___x_cmd csv tsv
    fi
}
# EndSection

# Section: website statistics stats
# https://umami.is/docs/api/website-stats#get-/api/websites/:websiteid/stats
___x_cmd_umami_site_stat(){
    [ "$#" -gt 0 ] || set -- --app

    # Check if --app, --raw, or --json is in arguments
    local op="--app"
    local has_mode=
    for _arg in "$@"; do
        case "$_arg" in
            --app|--raw|--json) has_mode=1; op="$_arg"; break ;;
        esac
    done

    # If no mode flag found, use default (tty gets --app, pipe gets --json)
    if [ -z "$has_mode" ]; then
        if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; then
            op="--app"
        else
            op="--json"
        fi
    fi

    # Build new args without mode flags
    local new_args=""
    for _arg in "$@"; do
        case "$_arg" in
            --app|--raw|--json) ;;
            *) new_args="${new_args}${new_args:+ }$_arg" ;;
        esac
    done

    # If no args left after removing mode flags, use website name
    if [ -z "$new_args" ]; then
        set -- x-cmd
    else
        set -- $new_args
    fi

    case "$op" in
        --app)        ___x_cmd_umami_site_stat___app   "$@" ;;
        --raw)        ___x_cmd_umami_site_stat___raw   "$@" ;;
        --json)       ___x_cmd_umami_site_stat___json  "$@" ;;
        -h|--help)    ___x_cmd help -m umami site stat        "$@" ;;
        *)            ___x_cmd_umami_site_stat___raw   "$op" "$@" ;;
    esac
}

___x_cmd_umami_site_stat___raw(){
    local arg=;local key=;
    local _start=; local _end=; local _type=

    # Parse website_id first
    local _website_id="$1";  [ -n "$_website_id" ] || M='Provide a websiteID'   N=umami log:ret:1
    ___x_cmd_umami___transform_website_id || return
    shift

    # Re-parse any remaining options that may have come after website_id
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --startAt)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _start=1
                shift 2;;
            --endAt)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _end=1
                shift 2;;
            --referrer|--title|--host|--os|--brower|\
            --device|--country|--region|--city|--query)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                shift 2;;
            *)  shift ;;
        esac
    done

    [ -n "$_start" ] || arg="startAt=$(___x_cmd_umami___date -15)&${arg}"
    [ -n "$_end" ]   || arg="endAt=$(___x_cmd_umami___date)&${arg}"
    arg="?${arg}"

    ___x_cmd_umami_api get "$___X_CMD_UMAMI___API_ENDPOINT/${_website_id}/stats${arg}"
}

___x_cmd_umami_site_stat___json(){
    ___x_cmd_umami_site_stat___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_umami_site_stat___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_umami_site_stat___raw "$@" | ___x_cmd jo fmt | ___x_cmd csv app
    else
        ___x_cmd_umami_site_stat___tsv "$@"
    fi
}

___x_cmd_umami_site_stat___tsv(){
    local stat_data
    stat_data="$(___x_cmd_umami_site_stat___raw "$@")" || return
    printf "pageviews\tvisitors\tvisits\tbounces\ttotaltime\n"
    printf "%s\t%s\t%s\t%s\t%s\n" \
        "$(printf '%s' "$stat_data" | grep -o '"pageviews":{"value":[0-9]*' | grep -o '[0-9]*')" \
        "$(printf '%s' "$stat_data" | grep -o '"visitors":{"value":[0-9]*' | grep -o '[0-9]*')" \
        "$(printf '%s' "$stat_data" | grep -o '"visits":{"value":[0-9]*' | grep -o '[0-9]*')" \
        "$(printf '%s' "$stat_data" | grep -o '"bounces":{"value":[0-9]*' | grep -o '[0-9]*')" \
        "$(printf '%s' "$stat_data" | grep -o '"totaltime":{"value":[0-9]*' | grep -o '[0-9]*')"
}
# EndSection

# Section: website statistics metrics
# https://umami.is/docs/api/website-stats#get-/api/websites/:websiteid/metrics
___x_cmd_umami_site_metric(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift
    case "$op" in
        --app)        ___x_cmd_umami_site_metric___app   "$@" ;;
        --raw)        ___x_cmd_umami_site_metric___raw   "$@" ;;
        --json)       ___x_cmd_umami_site_metric___json  "$@" ;;
        -h|--help)    ___x_cmd help -m umami site metric        "$@" ;;
        *)            ___x_cmd_umami_site_metric___raw   "$op" "$@" ;;
    esac
}

___x_cmd_umami_site_metric___raw(){
    local arg=;local key=;
    local _start=; local _end=;
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --startAt)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _start=1
                shift 2;;
            --endAt)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _end=1
                shift 2;;
            --type)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                _type=1
                shift 2;;
            --referrer|--title|--host|--os|--brower|\
            --device|--country|--region|--city|\
            --query|--language|--event|--limit)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                shift 2;;
            *)
                break ;;
        esac
    done
    [ -n "$_start" ] || arg="startAt=$(___x_cmd_umami___date -15)&${arg}"
    [ -n "$_end" ]   || arg="endAt=$(___x_cmd_umami___date)&${arg}"
    [ -n "$_type" ]  || M="Provide a type" N=umami log:ret:1
    arg="?${arg}"

    # Parse website_id first
    local _website_id="$1";  [ -n "$_website_id" ] || M='Provide a websiteID'   N=umami log:ret:1
    ___x_cmd_umami___transform_website_id || return
    shift

    # Re-parse any remaining options that may have come after website_id
    while [ "$#" -gt 0 ]; do
        case "$1" in
            --startAt|--endAt|--type|--referrer|--title|--host|--os|--brower|\
            --device|--country|--region|--city|--query|--language|--event|--limit)
                key="${1#--}"
                [ -n "$2" ] || M="Provide a $key" N=umami log:ret:1
                arg="${arg}&${key}=${2}"
                shift 2;;
            *)  shift ;;
        esac
    done

    ___x_cmd_umami_api get "$___X_CMD_UMAMI___API_ENDPOINT/${_website_id}/metrics${arg}"
}

___x_cmd_umami_site_metric___json(){
    ___x_cmd_umami_site_metric___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_umami_site_metric___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_umami_site_metric___raw "$@" | ___x_cmd jo fmt | ___x_cmd csv app
    else
        # Pipe output uses tsv by default
        printf "path\tcount\n"
        ___x_cmd_umami_site_metric___raw "$@" \
            | tr '{' '\n' \
            | grep '"x"' \
            | sed 's/.*"x":"\([^"]*\)".*"y":\([0-9]*\).*/\1\t\2/'
    fi
}
# EndSection
