#compdef mkv2cast
# mkv2cast zsh completion script
# Copyright (C) 2024-2026 voldardard
# License: GPL-3.0
#
# Installation:
#   Copy to ~/.local/share/zsh/site-functions/_mkv2cast
#   Or add to your fpath in ~/.zshrc:
#     fpath=(~/.local/share/zsh/site-functions $fpath)

_mkv2cast() {
    local curcontext="$curcontext" state line
    typeset -A opt_args
    
    local -a general_options output_options scan_options debug_options
    local -a codec_options quality_options hw_options integrity_options
    local -a ui_options pipeline_options parallel_options utility_options
    
    # General options
    general_options=(
        '(-h --help)'{-h,--help}'[Show help message and exit]'
        '(-V --version)'{-V,--version}'[Show version, author, and license]'
    )
    
    # Output settings
    output_options=(
        '--suffix=[Output file suffix]:suffix:(.cast .converted .chromecast)'
        '--container=[Output container format]:format:(mkv mp4)'
    )
    
    # Scan settings
    scan_options=(
        '(-r --recursive)'{-r,--recursive}'[Scan directories recursively (default)]'
        '--no-recursive[Disable recursive scanning]'
        '*'{-I,--ignore-pattern}'[Ignore files matching pattern]:pattern:'
        '*--ignore-path[Ignore specific paths or folders]:path:_files -/'
        '*'{-i,--include-pattern}'[Only process files matching pattern]:pattern:'
        '*--include-path[Only process files in matching paths]:path:_files -/'
    )
    
    # Debug/test options
    debug_options=(
        '(-d --debug)'{-d,--debug}'[Enable debug output]'
        '(-n --dryrun)'{-n,--dryrun}'[Dry run - show commands without executing]'
    )
    
    # Codec decisions
    codec_options=(
        '--skip-when-ok[Skip files already compatible (default)]'
        '--no-skip-when-ok[Process all files even if compatible]'
        '--force-h264[Force H264 transcoding even if already H264]'
        '--allow-hevc[Allow HEVC passthrough for SDR 8-bit content]'
        '--force-aac[Force AAC transcoding even if already AAC]'
        '--keep-surround[Keep surround audio channels]'
        '--no-silence[Do not add silence track if no audio]'
    )
    
    # Encoding quality
    quality_options=(
        '--abr=[Audio bitrate]:bitrate:(128k 160k 192k 256k 320k)'
        '--crf=[CRF value for CPU encoding]:crf:(18 19 20 21 22 23 24 25 26 28)'
        '--preset=[x264 encoding preset]:preset:(ultrafast superfast veryfast faster fast medium slow slower veryslow)'
    )
    
    # Hardware acceleration
    hw_options=(
        '--hw=[Hardware acceleration backend]:backend:(auto vaapi qsv cpu)'
        '--vaapi-device=[VAAPI device path]:device:_files -g "/dev/dri/*"'
        '--vaapi-qp=[VAAPI quantization parameter]:qp:(18 19 20 21 22 23 24 25 26 28)'
        '--qsv-quality=[QSV quality value]:quality:(18 19 20 21 22 23 24 25 26 28)'
    )
    
    # Integrity checks
    integrity_options=(
        '--integrity-check[Enable integrity check (default)]'
        '--no-integrity-check[Disable integrity check]'
        '--stable-wait=[Seconds to wait for file stability]:seconds:(1 2 3 4 5 10)'
        '--deep-check[Enable deep decode check (slow)]'
    )
    
    # UI settings
    ui_options=(
        '--no-progress[Disable progress bar]'
        '--bar-width=[Progress bar width]:width:(20 25 26 30 40 50)'
        '--ui-refresh-ms=[UI refresh interval in ms]:ms:(50 100 120 150 200 250)'
        '--stats-period=[FFmpeg stats period]:seconds:(0.1 0.2 0.5 1.0)'
    )
    
    # Pipeline mode
    pipeline_options=(
        '--pipeline[Enable parallel processing (default)]'
        '--no-pipeline[Disable parallel processing]'
    )
    
    # Parallelism
    parallel_options=(
        '--encode-workers=[Number of parallel encodes]:workers:(0 1 2 3 4 5 6)'
        '--integrity-workers=[Number of parallel integrity checks]:workers:(0 1 2 3 4 5 6)'
    )
    
    # Notifications
    local -a notify_options
    notify_options=(
        '--notify[Send desktop notification when done (default)]'
        '--no-notify[Disable desktop notifications]'
    )
    
    # Internationalization
    local -a i18n_options
    i18n_options=(
        '--lang=[Force language]:language:(en fr es it de)'
    )
    
    # Utility commands
    utility_options=(
        '--show-dirs[Show XDG directory paths and exit]'
        '--history=-[Show recent conversion history (default 20 lines)]:lines:(10 20 50 100 200 500 1000)'
        '--history-stats[Show conversion statistics and exit]'
        '--clean-tmp[Clean orphaned temp files and exit (all users if root)]'
        '--clean-logs=[Remove logs older than N days (all users if root)]:days:(7 14 30 60 90)'
        '--clean-history=[Remove history older than N days]:days:(7 14 30 60 90)'
        '--check-requirements[Check system requirements and exit]'
    )
    
    _arguments -C \
        $general_options \
        $output_options \
        $scan_options \
        $debug_options \
        $codec_options \
        $quality_options \
        $hw_options \
        $integrity_options \
        $ui_options \
        $pipeline_options \
        $parallel_options \
        $notify_options \
        $i18n_options \
        $utility_options \
        '*:MKV file:_files -g "*.mkv(-.)"' \
        && return 0
    
    return 1
}

# Complete .mkv files for positional arguments
_mkv2cast_mkv_files() {
    _files -g '*.mkv(-.)'
}

_mkv2cast "$@"
