"""Completion command for openai-tts-tool.

Note: This code was generated with assistance from AI coding tools
and has been reviewed and tested by a human.
"""

import click
from click.shell_completion import BashComplete, FishComplete, ShellComplete, ZshComplete


@click.command(name="completion")
@click.argument("shell", type=click.Choice(["bash", "zsh", "fish"], case_sensitive=False))
def completion_command(shell: str) -> None:
    """Generate shell completion script.

    SHELL: The shell type (bash, zsh, fish)

    Install instructions:

    \b
    # Bash (add to ~/.bashrc):
    eval "$(openai-tts-tool completion bash)"

    \b
    # Zsh (add to ~/.zshrc):
    eval "$(openai-tts-tool completion zsh)"

    \b
    # Fish (add to ~/.config/fish/completions/openai-tts-tool.fish):
    openai-tts-tool completion fish > ~/.config/fish/completions/openai-tts-tool.fish

    \b
    File-based Installation (Recommended for better performance):

    \b
    # Bash
    openai-tts-tool completion bash > ~/.openai-tts-tool-complete.bash
    echo 'source ~/.openai-tts-tool-complete.bash' >> ~/.bashrc

    \b
    # Zsh
    openai-tts-tool completion zsh > ~/.openai-tts-tool-complete.zsh
    echo 'source ~/.openai-tts-tool-complete.zsh' >> ~/.zshrc

    \b
    # Fish (automatic loading)
    mkdir -p ~/.config/fish/completions
    openai-tts-tool completion fish > ~/.config/fish/completions/openai-tts-tool.fish

    \b
    Supported Shells:
      - Bash (≥ 4.4)
      - Zsh (any recent version)
      - Fish (≥ 3.0)

    \b
    Note: PowerShell is not currently supported by Click's completion system.
    """
    ctx = click.get_current_context()

    # Get the appropriate completion class
    completion_classes: dict[str, type[ShellComplete]] = {
        "bash": BashComplete,
        "zsh": ZshComplete,
        "fish": FishComplete,
    }

    completion_class = completion_classes.get(shell.lower())
    if completion_class:
        completer = completion_class(
            cli=ctx.find_root().command,
            ctx_args={},
            prog_name="openai-tts-tool",
            complete_var="_OPENAI_TTS_TOOL_COMPLETE",
        )
        click.echo(completer.source())
    else:
        raise click.BadParameter(f"Unsupported shell: {shell}")
