"""API endpoints for retrieving image data (original, reference)."""

import logging
from typing import Literal

import albumentations as A
from fastapi import APIRouter, HTTPException

from src.models.image import ImageData
from src.services.image_service import ImageService

router = APIRouter(tags=["images"])
image_service = ImageService()

original_image_targets = Literal["image", "mask", "bboxes", "keypoints"]

logger = logging.getLogger(__name__)


@router.get("/original-data/{transform_name}")
async def get_original_data(transform_name: str) -> ImageData:
    """Get original image data with appropriate visualizations."""
    # Get transform class
    transform_class = getattr(A, transform_name, None)
    if not transform_class:
        raise HTTPException(status_code=404, detail=f"Transform {transform_name} not found")

    # Use the create_original_image_data method which handles everything
    return image_service.get_original_image_data(transform_name=transform_name)


@router.get("/reference-data/{transform_name}")
async def get_reference_data(transform_name: str | None = None) -> list[ImageData]:
    """Get reference/secondary image(s) data required for a transform."""
    transform_class = None
    if transform_name:
        # Get transform class - keep validation
        transform_class = getattr(A, transform_name, None)
        logger.info("Getting reference data for transform: %s (Class: %s)", transform_name, transform_class)
        if not transform_class:  # Check transform_name exists before getattr
            raise HTTPException(status_code=404, detail=f"Transform {transform_name} not found")

    return image_service.get_reference_image_data(transform_name=transform_name)
