# shellcheck shell=dash

# Section: list pull request creators
# https://ossinsight.io/docs/api/list-pull-request-creators
___x_cmd_ossinsight_pr_ls(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift

    local repo=""
    local period="30d"
    local page=1
    local page_size=50

    while [ "$#" -gt 0 ]; do
        case "$1" in
            --repo)
                [ -n "$2" ] || { N=ossinsight M="Provide repo" log:ret:1; }
                repo="$2"; shift 2 ;;
            --period)
                [ -n "$2" ] || { N=ossinsight M="Provide period" log:ret:1; }
                period="$2"; shift 2 ;;
            --page)
                [ -n "$2" ] || { N=ossinsight M="Provide page number" log:ret:1; }
                page="$2"; shift 2 ;;
            --page-size)
                [ -n "$2" ] || { N=ossinsight M="Provide page size" log:ret:1; }
                page_size="$2"; shift 2 ;;
            --app|--raw|--csv|--tsv|--json|--yml|--no-header)
                break ;;
            -h|--help)
                ___x_cmd help -m ossinsight pr ls "$@"
                return 0
                ;;
            *)
                [ -z "$repo" ] && repo="$1"; shift ;;
        esac
    done

    [ -n "$repo" ] || { N=ossinsight M="Provide repo (e.g. owner/repo)" log:ret:1; }

    case "$op" in
        --app)        ___x_cmd_ossinsight_pr_ls___app   "$@" ;;
        --raw)        ___x_cmd_ossinsight_pr_ls___raw   "$@" ;;
        --csv)        ___x_cmd_ossinsight_pr_ls___csv   "$@" ;;
        --tsv)        ___x_cmd_ossinsight_pr_ls___tsv   "$@" ;;
        --json)       ___x_cmd_ossinsight_pr_ls___json  "$@" ;;
        --yml)        ___x_cmd_ossinsight_pr_ls___yml   "$@" ;;
        --no-header)  ___x_cmd_ossinsight_pr_ls___csv --no-header "$@" ;;
        *)            ___x_cmd_ossinsight_pr_ls___raw   "$@" ;;
    esac
}

___x_cmd_ossinsight_pr_ls___raw(){
    local arg="repo=${repo}&period=${period}&page=${page}&page_size=${page_size}"
    ___x_cmd_ossinsight_api get "/pull-request-creators?${arg}"
}

___x_cmd_ossinsight_pr_ls___json(){
    ___x_cmd_ossinsight_pr_ls___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_ossinsight_pr_ls___yml(){
    ___x_cmd_ossinsight_pr_ls___raw "$@" | ___x_cmd j2y
}

___x_cmd_ossinsight_pr_ls___csv(){
    local csv_flags="--add"
    case "$1" in
        --no-header) csv_flags=""; shift ;;
    esac
    ___x_cmd_ossinsight_pr_ls___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .rank .user_id .login .avatar_url .pull_requests .merged .percentage \
        | ___x_cmd csv header $csv_flags rank user_id login avatar_url pull_requests merged percentage
}

___x_cmd_ossinsight_pr_ls___tsv(){
    local print_header=1
    case "$1" in
        --no-header) print_header=""; shift ;;
    esac
    if [ -n "$print_header" ]; then
        printf "rank\tuser_id\tlogin\tavatar_url\tpull_requests\tmerged\tpercentage\n"
    fi
    ___x_cmd_ossinsight_pr_ls___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .rank .user_id .login .avatar_url .pull_requests .merged .percentage \
        | tr ',' '\t'
}

___x_cmd_ossinsight_pr_ls___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_ossinsight_pr_ls___csv "$@" | ___x_cmd csv app
    else
        ___x_cmd_ossinsight_pr_ls___tsv "$@"
    fi
}
# EndSection

# Section: list countries of PR creators
# https://ossinsight.io/docs/api/list-countries-of-pr-creators
___x_cmd_ossinsight_pr_countries(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift

    local repo=""
    local period="30d"

    while [ "$#" -gt 0 ]; do
        case "$1" in
            --repo)
                [ -n "$2" ] || { N=ossinsight M="Provide repo" log:ret:1; }
                repo="$2"; shift 2 ;;
            --period)
                [ -n "$2" ] || { N=ossinsight M="Provide period" log:ret:1; }
                period="$2"; shift 2 ;;
            --app|--raw|--csv|--tsv|--json|--yml|--no-header)
                break ;;
            -h|--help)
                ___x_cmd help -m ossinsight pr countries "$@"
                return 0
                ;;
            *)
                [ -z "$repo" ] && repo="$1"; shift ;;
        esac
    done

    [ -n "$repo" ] || { N=ossinsight M="Provide repo (e.g. owner/repo)" log:ret:1; }

    case "$op" in
        --app)        ___x_cmd_ossinsight_pr_countries___app   "$@" ;;
        --raw)        ___x_cmd_ossinsight_pr_countries___raw   "$@" ;;
        --csv)        ___x_cmd_ossinsight_pr_countries___csv   "$@" ;;
        --tsv)        ___x_cmd_ossinsight_pr_countries___tsv   "$@" ;;
        --json)       ___x_cmd_ossinsight_pr_countries___json  "$@" ;;
        --yml)        ___x_cmd_ossinsight_pr_countries___yml   "$@" ;;
        --no-header)  ___x_cmd_ossinsight_pr_countries___csv --no-header "$@" ;;
        *)            ___x_cmd_ossinsight_pr_countries___raw   "$@" ;;
    esac
}

___x_cmd_ossinsight_pr_countries___raw(){
    local arg="repo=${repo}&period=${period}"
    ___x_cmd_ossinsight_api get "/pull-request-creators/countries?${arg}"
}

___x_cmd_ossinsight_pr_countries___json(){
    ___x_cmd_ossinsight_pr_countries___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_ossinsight_pr_countries___yml(){
    ___x_cmd_ossinsight_pr_countries___raw "$@" | ___x_cmd j2y
}

___x_cmd_ossinsight_pr_countries___csv(){
    local csv_flags="--add"
    case "$1" in
        --no-header) csv_flags=""; shift ;;
    esac
    ___x_cmd_ossinsight_pr_countries___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .country_code .country_name .count .percentage \
        | ___x_cmd csv header $csv_flags country_code country_name count percentage
}

___x_cmd_ossinsight_pr_countries___tsv(){
    local print_header=1
    case "$1" in
        --no-header) print_header=""; shift ;;
    esac
    if [ -n "$print_header" ]; then
        printf "country_code\tcountry_name\tcount\tpercentage\n"
    fi
    ___x_cmd_ossinsight_pr_countries___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .country_code .country_name .count .percentage \
        | tr ',' '\t'
}

___x_cmd_ossinsight_pr_countries___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_ossinsight_pr_countries___csv "$@" | ___x_cmd csv app
    else
        ___x_cmd_ossinsight_pr_countries___tsv "$@"
    fi
}
# EndSection

# Section: PR creators history
# https://ossinsight.io/docs/api/pull-request-creators-history
___x_cmd_ossinsight_pr_history(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift

    local repo=""
    local period="30d"

    while [ "$#" -gt 0 ]; do
        case "$1" in
            --repo)
                [ -n "$2" ] || { N=ossinsight M="Provide repo" log:ret:1; }
                repo="$2"; shift 2 ;;
            --period)
                [ -n "$2" ] || { N=ossinsight M="Provide period" log:ret:1; }
                period="$2"; shift 2 ;;
            --app|--raw|--csv|--tsv|--json|--yml|--no-header)
                break ;;
            -h|--help)
                ___x_cmd help -m ossinsight pr history "$@"
                return 0
                ;;
            *)
                [ -z "$repo" ] && repo="$1"; shift ;;
        esac
    done

    [ -n "$repo" ] || { N=ossinsight M="Provide repo (e.g. owner/repo)" log:ret:1; }

    case "$op" in
        --app)        ___x_cmd_ossinsight_pr_history___app   "$@" ;;
        --raw)        ___x_cmd_ossinsight_pr_history___raw   "$@" ;;
        --csv)        ___x_cmd_ossinsight_pr_history___csv   "$@" ;;
        --tsv)        ___x_cmd_ossinsight_pr_history___tsv   "$@" ;;
        --json)       ___x_cmd_ossinsight_pr_history___json  "$@" ;;
        --yml)        ___x_cmd_ossinsight_pr_history___yml   "$@" ;;
        --no-header)  ___x_cmd_ossinsight_pr_history___csv --no-header "$@" ;;
        *)            ___x_cmd_ossinsight_pr_history___raw   "$@" ;;
    esac
}

___x_cmd_ossinsight_pr_history___raw(){
    local arg="repo=${repo}&period=${period}"
    ___x_cmd_ossinsight_api get "/pull-request-creators/history?${arg}"
}

___x_cmd_ossinsight_pr_history___json(){
    ___x_cmd_ossinsight_pr_history___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_ossinsight_pr_history___yml(){
    ___x_cmd_ossinsight_pr_history___raw "$@" | ___x_cmd j2y
}

___x_cmd_ossinsight_pr_history___csv(){
    local csv_flags="--add"
    case "$1" in
        --no-header) csv_flags=""; shift ;;
    esac
    ___x_cmd_ossinsight_pr_history___raw "$@" \
        | ___x_cmd jo 1.data \
        | ___x_cmd jo 2c .date .count .merged .increment \
        | ___x_cmd csv header $csv_flags date count merged increment
}

___x_cmd_ossinsight_pr_history___tsv(){
    local print_header=1
    case "$1" in
        --no-header) print_header=""; shift ;;
    esac
    if [ -n "$print_header" ]; then
        printf "date\tcount\tmerged\tincrement\n"
    fi
    ___x_cmd_ossinsight_pr_history___raw "$@" \
        | ___x_cmd jo 1.data \
        | ___x_cmd jo 2c .date .count .merged .increment \
        | tr ',' '\t'
}

___x_cmd_ossinsight_pr_history___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_ossinsight_pr_history___csv "$@" | ___x_cmd csv app
    else
        ___x_cmd_ossinsight_pr_history___tsv "$@"
    fi
}
# EndSection

# Section: list organizations of PR creators
# https://ossinsight.io/docs/api/list-organizations-of-pr-creators
___x_cmd_ossinsight_pr_orgs(){
    [ "$#" -gt 0 ] || set -- --app
    local op="$1"; shift

    local repo=""
    local period="30d"
    local page=1
    local page_size=50

    while [ "$#" -gt 0 ]; do
        case "$1" in
            --repo)
                [ -n "$2" ] || { N=ossinsight M="Provide repo" log:ret:1; }
                repo="$2"; shift 2 ;;
            --period)
                [ -n "$2" ] || { N=ossinsight M="Provide period" log:ret:1; }
                period="$2"; shift 2 ;;
            --page)
                [ -n "$2" ] || { N=ossinsight M="Provide page number" log:ret:1; }
                page="$2"; shift 2 ;;
            --page-size)
                [ -n "$2" ] || { N=ossinsight M="Provide page size" log:ret:1; }
                page_size="$2"; shift 2 ;;
            --app|--raw|--csv|--tsv|--json|--yml|--no-header)
                break ;;
            -h|--help)
                ___x_cmd help -m ossinsight pr orgs "$@"
                return 0
                ;;
            *)
                [ -z "$repo" ] && repo="$1"; shift ;;
        esac
    done

    [ -n "$repo" ] || { N=ossinsight M="Provide repo (e.g. owner/repo)" log:ret:1; }

    case "$op" in
        --app)        ___x_cmd_ossinsight_pr_orgs___app   "$@" ;;
        --raw)        ___x_cmd_ossinsight_pr_orgs___raw   "$@" ;;
        --csv)        ___x_cmd_ossinsight_pr_orgs___csv   "$@" ;;
        --tsv)        ___x_cmd_ossinsight_pr_orgs___tsv   "$@" ;;
        --json)       ___x_cmd_ossinsight_pr_orgs___json  "$@" ;;
        --yml)        ___x_cmd_ossinsight_pr_orgs___yml   "$@" ;;
        --no-header)  ___x_cmd_ossinsight_pr_orgs___csv --no-header "$@" ;;
        *)            ___x_cmd_ossinsight_pr_orgs___raw   "$@" ;;
    esac
}

___x_cmd_ossinsight_pr_orgs___raw(){
    local arg="repo=${repo}&period=${period}&page=${page}&page_size=${page_size}"
    ___x_cmd_ossinsight_api get "/pull-request-creators/orgs?${arg}"
}

___x_cmd_ossinsight_pr_orgs___json(){
    ___x_cmd_ossinsight_pr_orgs___raw "$@" | ___x_cmd jo fmt
}

___x_cmd_ossinsight_pr_orgs___yml(){
    ___x_cmd_ossinsight_pr_orgs___raw "$@" | ___x_cmd j2y
}

___x_cmd_ossinsight_pr_orgs___csv(){
    local csv_flags="--add"
    case "$1" in
        --no-header) csv_flags=""; shift ;;
    esac
    ___x_cmd_ossinsight_pr_orgs___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .organization .count .percentage \
        | ___x_cmd csv header $csv_flags organization count percentage
}

___x_cmd_ossinsight_pr_orgs___tsv(){
    local print_header=1
    case "$1" in
        --no-header) print_header=""; shift ;;
    esac
    if [ -n "$print_header" ]; then
        printf "organization\tcount\tpercentage\n"
    fi
    ___x_cmd_ossinsight_pr_orgs___raw "$@" \
        | ___x_cmd jo 1.data.rows \
        | ___x_cmd jo 2c .organization .count .percentage \
        | tr ',' '\t'
}

___x_cmd_ossinsight_pr_orgs___app(){
    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual ; then
        ___x_cmd_ossinsight_pr_orgs___csv "$@" | ___x_cmd csv app
    else
        ___x_cmd_ossinsight_pr_orgs___tsv "$@"
    fi
}
# EndSection