"""
AI Processor - Utility Functions

Image encoding and helper functions.
"""

from pathlib import Path
import base64
import shutil
import logging

from .config import BEREINIGT_DIR

logger = logging.getLogger(__name__)


def encode_image_to_data_url(input_path: Path) -> str:
    """
    Encode an image file to a data URL for API requests.

    Args:
        input_path: Path to the image file

    Returns:
        Data URL string (data:image/jpeg;base64,...)
    """
    image_bytes = input_path.read_bytes()
    image_b64 = base64.b64encode(image_bytes).decode("utf-8")
    return f"data:image/jpeg;base64,{image_b64}"


def encode_image_to_base64(input_path: Path) -> str:
    """
    Encode an image file to base64 string.

    Args:
        input_path: Path to the image file

    Returns:
        Base64 encoded string
    """
    image_bytes = input_path.read_bytes()
    return base64.b64encode(image_bytes).decode("utf-8")


def dummy_process_scan(input_path: Path) -> Path:
    """
    Simple copy to Bereinigt folder - kept as fallback.

    Args:
        input_path: Path to input file

    Returns:
        Path to copied file
    """
    if not input_path.exists():
        raise FileNotFoundError(f"Eingabedatei nicht gefunden: {input_path}")
    new_name = input_path.stem + "_bereinigt" + input_path.suffix
    target = BEREINIGT_DIR / new_name
    shutil.copy2(input_path, target)
    return target


def get_media_type(input_path: Path) -> str:
    """
    Determine media type from file extension.

    Args:
        input_path: Path to the file

    Returns:
        MIME type string
    """
    suffix = input_path.suffix.lower()
    media_types = {
        ".jpg": "image/jpeg",
        ".jpeg": "image/jpeg",
        ".png": "image/png",
        ".gif": "image/gif",
        ".webp": "image/webp",
        ".pdf": "application/pdf",
    }
    return media_types.get(suffix, "image/jpeg")
