from fastapi import APIRouter, HTTPException

from math_problems_structure.core.problems.grade_1 import (
    compose_and_decompose_numbers,
    join_corresponding_sums,
    join_pictures_with_quantity,
    subtract_with_image_reference,
    sum_with_image_reference,
    where_are_more_items,
)
from math_problems_structure.core.schemas.grade_1 import (
    ComposeAndDecomposeNumbersProblem,
    ComposeAndDecomposeNumbersRequest,
    JoinCorrespondingSumsProblem,
    JoinCorrespondingSumsRequest,
    JoinPicturesWithQuantityProblem,
    JoinPicturesWithQuantityRequest,
    SubtractWithImageReferenceProblem,
    SubtractWithImageReferenceRequest,
    SumWithImageReferenceProblem,
    SumWithImageReferenceRequest,
    WhereAreMoreItemsProblem,
    WhereAreMoreItemsRequest,
)

router = APIRouter(prefix="/grade-1", tags=["Grade 1"])


def _bad_request(error: ValueError) -> HTTPException:
    return HTTPException(status_code=400, detail=str(error))


@router.post(
    "/join-pictures-with-quantity",
    response_model=JoinPicturesWithQuantityProblem,
)
async def generate_join_pictures_with_quantity(
    request: JoinPicturesWithQuantityRequest,
) -> dict:
    try:
        return join_pictures_with_quantity(**request.model_dump())
    except ValueError as error:
        raise _bad_request(error) from error


@router.post(
    "/join-corresponding-sums",
    response_model=JoinCorrespondingSumsProblem,
)
async def generate_join_corresponding_sums(
    request: JoinCorrespondingSumsRequest,
) -> dict:
    try:
        return join_corresponding_sums(**request.model_dump())
    except ValueError as error:
        raise _bad_request(error) from error


@router.post(
    "/sum-with-image-reference",
    response_model=SumWithImageReferenceProblem,
)
async def generate_sum_with_image_reference(
    request: SumWithImageReferenceRequest,
) -> dict:
    try:
        return sum_with_image_reference(**request.model_dump())
    except ValueError as error:
        raise _bad_request(error) from error


@router.post(
    "/subtract-with-image-reference",
    response_model=SubtractWithImageReferenceProblem,
)
async def generate_subtract_with_image_reference(
    request: SubtractWithImageReferenceRequest,
) -> dict:
    try:
        return subtract_with_image_reference(**request.model_dump())
    except ValueError as error:
        raise _bad_request(error) from error


@router.post(
    "/compose-and-decompose-numbers",
    response_model=ComposeAndDecomposeNumbersProblem,
)
async def generate_compose_and_decompose_numbers(
    request: ComposeAndDecomposeNumbersRequest,
) -> dict:
    try:
        return compose_and_decompose_numbers(**request.model_dump())
    except ValueError as error:
        raise _bad_request(error) from error


@router.post(
    "/where-are-more-items",
    response_model=WhereAreMoreItemsProblem,
)
async def generate_where_are_more_items(
    request: WhereAreMoreItemsRequest,
) -> dict:
    try:
        return where_are_more_items(**request.model_dump())
    except ValueError as error:
        raise _bad_request(error) from error
