#compdef ox-cli

_ox_cli() {
    local -a commands
    local curcontext="$curcontext" state line ret=1
    typeset -A opt_args

    commands=(
        'scan:Scan the repository for security issues'
        'scan-result:Get the results of a specific scan by ID'
        'install-git-hook:Install a git hook that runs ox-cli scan'
        'uninstall-git-hook:Remove a git hook previously installed by ox-cli'
        'update:Update the CLI to a specific or latest version from npm'
        'config:Manage global configuration'
    )

    _arguments -C \
        '(-h --help)'{-h,--help}'[Show help information]' \
        '(-v --version)'{-v,--version}'[Show version]' \
        '1: :->command' \
        '*:: :->args' &&
        ret=0

    case $state in
    command)
        _describe -t commands 'ox-cli commands' commands && ret=0
        ;;
    args)
        case $line[1] in
        scan)
            _arguments \
                '1:target directory:_files -/' \
                '(-f --format)'{-f,--format}'[Output format]:format:(text json sarif)' \
                '--severity[Comma-separated list of severities]:severities:(Critical,High Critical,High,Medium Critical,High,Medium,Low Critical,High,Medium,Low,Info)' \
                '--group[Group results by]:grouping:(severity category)' \
                '--git-branch-name[Git branch name]:branch name:' \
                '--git-remote-name[Git remote name]:remote name:' \
                '--force-branch-scan[Scan the remote branch if no local changes]' &&
                ret=0
            ;;
        scan-result)
            _arguments \
                '1:scan ID:' \
                '2:target directory:_files -/' \
                '(-f --format)'{-f,--format}'[Output format]:format:(text json sarif)' \
                '--severity[Comma-separated list of severities]:severities:(Critical,High Critical,High,Medium Critical,High,Medium,Low Critical,High,Medium,Low,Info Appoxalypse)' \
                '--group[Group results by]:grouping:(severity category)' &&
                ret=0
            ;;
        install-git-hook)
            _arguments \
                '1:target directory:_files -/' \
                '--type[Hook type]:hook type:(pre-push pre-commit)' \
                '--force[Overwrite existing hook if present]' &&
                ret=0
            ;;
        uninstall-git-hook)
            _arguments \
                '1:target directory:_files -/' \
                '--type[Hook type]:hook type:(pre-push pre-commit)' &&
                ret=0
            ;;
        update)
            _arguments \
                '(-v --version)'{-v,--version}'[Target version to update to]:version:' &&
                ret=0
            ;;
        config)
            local -a config_cmds
            config_cmds=(
                'set:Set a configuration value'
                'get:Get a configuration value'
            )

            if ((CURRENT == 2)); then
                _describe -t config_cmds 'config subcommands' config_cmds && ret=0
                _message 'API key value (or leave empty to be prompted)' && ret=0
            elif ((CURRENT > 2)); then
                case $line[2] in
                set)
                    if ((CURRENT == 3)); then
                        _values 'setting' 'api-key' 'api-host' && ret=0
                    elif ((CURRENT == 4)); then
                        _message 'value (or leave empty to be prompted)' && ret=0
                    fi
                    ;;
                get)
                    if ((CURRENT == 3)); then
                        _values 'setting' 'api-key' 'api-host' && ret=0
                    fi
                    ;;
                esac
            fi
            ;;
        esac
        ;;
    esac

    return ret
}

_ox_cli "$@"
