"""Path validation and security utilities."""

from __future__ import annotations

from pathlib import Path

from pptx_mcp.utils.errors import PathValidationError

ALLOWED_OUTPUT_EXTENSIONS = {".pptx"}
ALLOWED_INPUT_EXTENSIONS = {".pptx"}
ALLOWED_CONFIG_EXTENSIONS = {".json", ".yaml", ".yml"}
ALLOWED_IMAGE_EXTENSIONS = {
    ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".svg", ".emf", ".wmf",
}
ALLOWED_TEMPLATE_EXTENSIONS = {".pptx"}

MAX_PATH_LENGTH = 1024


def validate_output_path(path: str | Path) -> Path:
    """Validate and resolve an output file path.

    Checks:
      - Path length within bounds
      - Extension is .pptx
      - Parent directory exists or can be created

    Returns:
        Resolved Path object.

    Raises:
        PathValidationError: If validation fails.
    """
    if len(str(path)) > MAX_PATH_LENGTH:
        raise PathValidationError("Output path exceeds maximum length")

    path = Path(path).resolve()

    if path.suffix.lower() not in ALLOWED_OUTPUT_EXTENSIONS:
        raise PathValidationError(
            f"Output file must have a .pptx extension, got '{path.suffix}'"
        )

    return path


def validate_input_path(
    path: str | Path,
    allowed_extensions: set[str] | None = None,
) -> Path:
    """Validate an input file path exists and has an allowed extension.

    Returns:
        Resolved Path object.

    Raises:
        PathValidationError: If validation fails.
    """
    if len(str(path)) > MAX_PATH_LENGTH:
        raise PathValidationError("Input path exceeds maximum length")

    path = Path(path).resolve()

    if not path.exists():
        raise PathValidationError(f"File not found: {path.name}")

    if not path.is_file():
        raise PathValidationError(f"Path is not a file: {path.name}")

    if allowed_extensions and path.suffix.lower() not in allowed_extensions:
        raise PathValidationError(
            f"Unsupported file type '{path.suffix}'. "
            f"Allowed: {', '.join(sorted(allowed_extensions))}"
        )

    return path


def validate_pptx_input(path: str | Path) -> Path:
    """Validate a PPTX input file path."""
    return validate_input_path(path, ALLOWED_INPUT_EXTENSIONS)


def validate_config_path(path: str | Path) -> Path:
    """Validate a brand config file path."""
    return validate_input_path(path, ALLOWED_CONFIG_EXTENSIONS)


def validate_image_path(path: str | Path) -> Path:
    """Validate an image file path."""
    return validate_input_path(path, ALLOWED_IMAGE_EXTENSIONS)


def validate_template_path(path: str | Path) -> Path:
    """Validate a PPTX template file path."""
    return validate_input_path(path, ALLOWED_TEMPLATE_EXTENSIONS)
