"""Prompt integration script generation.

This module generates shell scripts that provide prompt integration
and helper functions for working with containers in the shell.
"""

from __future__ import annotations

from gaol.shell.completions import Shell


class PromptIntegration:
    """Generate prompt integration scripts.

    Generates shell scripts that provide:
    - gaol_prompt() - Returns container context string for PS1
    - muse <container> - Set current container context
    - munuse - Unset container context
    - msh - Quick shell into current container
    - mstatus - Show current container status
    - mexec - Execute command in current container

    Example:
        >>> prompt = PromptIntegration()
        >>> script = prompt.generate(Shell.BASH)
        >>> print(script)  # Add to ~/.bashrc
    """

    ENV_VAR = "GAOL_CONTAINER"

    def generate(self, shell: Shell | str) -> str:
        """Generate prompt integration script for the specified shell.

        Args:
            shell: Shell type (bash, zsh, or fish).

        Returns:
            Shell prompt integration script as string.

        Raises:
            ValueError: If shell type is not supported.
        """
        if isinstance(shell, str):
            shell = Shell(shell.lower())

        if shell == Shell.BASH:
            return self._generate_bash()
        elif shell == Shell.ZSH:
            return self._generate_zsh()
        elif shell == Shell.FISH:
            return self._generate_fish()
        else:
            raise ValueError(f"Unsupported shell: {shell}")

    def _generate_bash(self) -> str:
        """Generate Bash prompt integration script."""
        return f'''# Gaol shell integration for Bash
# Generated by gaol shell-integration
# Add to ~/.bashrc: eval "$(gaol shell-integration init bash)"

# Environment variable for current container context
export {self.ENV_VAR}="${{{self.ENV_VAR}:-}}"

# Get container context for prompt
# Usage in PS1: PS1='$(gaol_prompt)\\u@\\h:\\w\\$ '
gaol_prompt() {{
    if [ -n "${self.ENV_VAR}" ]; then
        local status
        status=$(gaol status "${self.ENV_VAR}" --format short 2>/dev/null)
        echo "[${{{self.ENV_VAR}}}:${{status:-?}}] "
    fi
}}

# Set current container context
# Usage: muse mycontainer
muse() {{
    if [ -z "$1" ]; then
        echo "Usage: muse <container>"
        echo "Set the current container context"
        return 1
    fi
    export {self.ENV_VAR}="$1"
    echo "Container context: $1"
}}

# Unset current container context
# Usage: munuse
munuse() {{
    unset {self.ENV_VAR}
    echo "Container context cleared"
}}

# Quick shell into current container
# Usage: msh [--user USER]
msh() {{
    if [ -z "${self.ENV_VAR}" ]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol shell "${self.ENV_VAR}" "$@"
}}

# Show current container status
# Usage: mstatus
mstatus() {{
    if [ -z "${self.ENV_VAR}" ]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol status "${self.ENV_VAR}"
}}

# Quick exec in current container
# Usage: mexec <command> [args...]
mexec() {{
    if [ -z "${self.ENV_VAR}" ]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    if [ $# -eq 0 ]; then
        echo "Usage: mexec <command> [args...]"
        return 1
    fi
    gaol exec "${self.ENV_VAR}" -- "$@"
}}

# Start current container
# Usage: mstart
mstart() {{
    if [ -z "${self.ENV_VAR}" ]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol start "${self.ENV_VAR}"
}}

# Stop current container
# Usage: mstop
mstop() {{
    if [ -z "${self.ENV_VAR}" ]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol stop "${self.ENV_VAR}"
}}

# Show logs for current container
# Usage: mlogs [options]
mlogs() {{
    if [ -z "${self.ENV_VAR}" ]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol logs "${self.ENV_VAR}" "$@"
}}
'''

    def _generate_zsh(self) -> str:
        """Generate Zsh prompt integration script."""
        return f'''# Gaol shell integration for Zsh
# Generated by gaol shell-integration
# Add to ~/.zshrc: eval "$(gaol shell-integration init zsh)"

# Environment variable for current container context
export {self.ENV_VAR}="${{{self.ENV_VAR}:-}}"

# Get container context for prompt
# Usage in PROMPT: PROMPT='$(gaol_prompt)%n@%m:%~%# '
gaol_prompt() {{
    if [[ -n "${self.ENV_VAR}" ]]; then
        local status
        status=$(gaol status "${self.ENV_VAR}" --format short 2>/dev/null)
        echo "[${{{self.ENV_VAR}}}:${{status:-?}}] "
    fi
}}

# Set current container context
# Usage: muse mycontainer
muse() {{
    if [[ -z "$1" ]]; then
        echo "Usage: muse <container>"
        echo "Set the current container context"
        return 1
    fi
    export {self.ENV_VAR}="$1"
    echo "Container context: $1"
}}

# Unset current container context
# Usage: munuse
munuse() {{
    unset {self.ENV_VAR}
    echo "Container context cleared"
}}

# Quick shell into current container
# Usage: msh [--user USER]
msh() {{
    if [[ -z "${self.ENV_VAR}" ]]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol shell "${self.ENV_VAR}" "$@"
}}

# Show current container status
# Usage: mstatus
mstatus() {{
    if [[ -z "${self.ENV_VAR}" ]]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol status "${self.ENV_VAR}"
}}

# Quick exec in current container
# Usage: mexec <command> [args...]
mexec() {{
    if [[ -z "${self.ENV_VAR}" ]]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    if [[ $# -eq 0 ]]; then
        echo "Usage: mexec <command> [args...]"
        return 1
    fi
    gaol exec "${self.ENV_VAR}" -- "$@"
}}

# Start current container
# Usage: mstart
mstart() {{
    if [[ -z "${self.ENV_VAR}" ]]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol start "${self.ENV_VAR}"
}}

# Stop current container
# Usage: mstop
mstop() {{
    if [[ -z "${self.ENV_VAR}" ]]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol stop "${self.ENV_VAR}"
}}

# Show logs for current container
# Usage: mlogs [options]
mlogs() {{
    if [[ -z "${self.ENV_VAR}" ]]; then
        echo "No container set. Use: muse <container>"
        return 1
    fi
    gaol logs "${self.ENV_VAR}" "$@"
}}
'''

    def _generate_fish(self) -> str:
        """Generate Fish prompt integration script."""
        return f'''# Gaol shell integration for Fish
# Generated by gaol shell-integration
# Add to ~/.config/fish/config.fish: gaol shell-integration init fish | source

# Get container context for prompt
# Usage in fish_prompt: set_color blue; gaol_prompt; set_color normal
function gaol_prompt
    if set -q {self.ENV_VAR}
        set -l container_status (gaol status ${self.ENV_VAR} --format short 2>/dev/null)
        if test -z "$container_status"
            set container_status "?"
        end
        echo "[${self.ENV_VAR}:$container_status] "
    end
end

# Set current container context
# Usage: muse mycontainer
function muse -a container -d "Set the current container context"
    if test -z "$container"
        echo "Usage: muse <container>"
        echo "Set the current container context"
        return 1
    end
    set -gx {self.ENV_VAR} $container
    echo "Container context: $container"
end

# Unset current container context
# Usage: munuse
function munuse -d "Clear the current container context"
    set -e {self.ENV_VAR}
    echo "Container context cleared"
end

# Quick shell into current container
# Usage: msh [--user USER]
function msh -d "Shell into current container"
    if not set -q {self.ENV_VAR}
        echo "No container set. Use: muse <container>"
        return 1
    end
    gaol shell ${self.ENV_VAR} $argv
end

# Show current container status
# Usage: mstatus
function mstatus -d "Show current container status"
    if not set -q {self.ENV_VAR}
        echo "No container set. Use: muse <container>"
        return 1
    end
    gaol status ${self.ENV_VAR}
end

# Quick exec in current container
# Usage: mexec <command> [args...]
function mexec -d "Execute command in current container"
    if not set -q {self.ENV_VAR}
        echo "No container set. Use: muse <container>"
        return 1
    end
    if test (count $argv) -eq 0
        echo "Usage: mexec <command> [args...]"
        return 1
    end
    gaol exec ${self.ENV_VAR} -- $argv
end

# Start current container
# Usage: mstart
function mstart -d "Start current container"
    if not set -q {self.ENV_VAR}
        echo "No container set. Use: muse <container>"
        return 1
    end
    gaol start ${self.ENV_VAR}
end

# Stop current container
# Usage: mstop
function mstop -d "Stop current container"
    if not set -q {self.ENV_VAR}
        echo "No container set. Use: muse <container>"
        return 1
    end
    gaol stop ${self.ENV_VAR}
end

# Show logs for current container
# Usage: mlogs [options]
function mlogs -d "Show logs for current container"
    if not set -q {self.ENV_VAR}
        echo "No container set. Use: muse <container>"
        return 1
    end
    gaol logs ${self.ENV_VAR} $argv
end
'''
