# Copyright (c) 2026 MergeGuide, Inc. All rights reserved.
# SPDX-License-Identifier: Proprietary
"""CLI error messages with helpful suggestions."""

from __future__ import annotations

from dataclasses import dataclass


@dataclass
class CLIError:
    """Structured CLI error with helpful context."""

    code: str
    title: str
    message: str
    suggestion: str | None = None
    doc_link: str | None = None


ERRORS = {
    "CONFIG_NOT_FOUND": CLIError(
        code="E001",
        title="Configuration not found",
        message="No .mergeguide.yaml or .mergeguide/config.yaml file found.",
        suggestion="Run `mergeguide init` to create configuration.",
    ),
    "NOT_GIT_REPO": CLIError(
        code="E002",
        title="Not a git repository",
        message="This command requires a git repository.",
        suggestion="Run `git init` or navigate to a git repository.",
    ),
    "POLICY_LOAD_ERROR": CLIError(
        code="E003",
        title="Failed to load policy",
        message="The policy file could not be parsed.",
        suggestion="Check the YAML syntax and ensure the file is valid.",
    ),
    "POLICY_NOT_FOUND": CLIError(
        code="E004",
        title="Policy file not found",
        message="The specified policy file does not exist.",
        suggestion="Check the file path and try again.",
    ),
    "INVALID_CONFIG_KEY": CLIError(
        code="E005",
        title="Invalid configuration key",
        message="The specified configuration key is not recognized.",
        suggestion="Run `mergeguide config list` to see available keys.",
    ),
    "CONFIG_WRITE_ERROR": CLIError(
        code="E006",
        title="Failed to write configuration",
        message="Could not write to the configuration file.",
        suggestion="Check file permissions and try again.",
    ),
    "HOOK_INSTALL_FAILED": CLIError(
        code="E007",
        title="Hook installation failed",
        message="Could not install the git hook.",
        suggestion="Check .git/hooks directory permissions.",
    ),
    "NO_FILES_TO_CHECK": CLIError(
        code="E008",
        title="No files to check",
        message="No source files found matching the specified paths.",
        suggestion="Specify files or directories containing source code.",
    ),
}


def format_error(error_key: str, **kwargs: str) -> str:
    """Format an error message for Rich console display.

    Args:
        error_key: Key from the ERRORS dictionary.
        **kwargs: Optional values to format into the message.

    Returns:
        Formatted error string with Rich markup.
    """
    error = ERRORS.get(error_key)
    if not error:
        return f"[red]Unknown error: {error_key}[/red]"

    message = error.message.format(**kwargs) if kwargs else error.message

    lines = [
        f"[bold red]{error.title}[/bold red] ({error.code})",
        "",
        message,
    ]

    if error.suggestion:
        lines.extend(["", f"[cyan]Suggestion:[/cyan] {error.suggestion}"])

    if error.doc_link:
        lines.extend(["", f"[dim]Documentation: {error.doc_link}[/dim]"])

    return "\n".join(lines)


def format_error_plain(error_key: str, **kwargs: str) -> str:
    """Format an error message for plain text output (no Rich markup).

    Args:
        error_key: Key from the ERRORS dictionary.
        **kwargs: Optional values to format into the message.

    Returns:
        Formatted error string without markup.
    """
    error = ERRORS.get(error_key)
    if not error:
        return f"Error: {error_key}"

    message = error.message.format(**kwargs) if kwargs else error.message

    lines = [
        f"Error {error.code}: {error.title}",
        message,
    ]

    if error.suggestion:
        lines.append(f"Suggestion: {error.suggestion}")

    return "\n".join(lines)
