from fastapi import APIRouter, HTTPException

from app.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 app.schemas.grade_1 import (
    ComposeAndDecomposeNumbersProblem,
    ComposeAndDecomposeNumbersRequest,
    JoinCorrespondingSumsProblem,
    JoinCorrespondingSumsRequest,
    JoinPicturesWithQuantityProblem,
    JoinPicturesWithQuantityRequest,
    SubtractWithImageReferenceProblem,
    SubtractWithImageReferenceRequest,
    SumWithImageReferenceProblem,
    SumWithImageReferenceRequest,
    WhereAreMoreItemsProblem,
    WhereAreMoreItemsRequest,
)

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


@router.post(
    "/subtract_with_image_reference",
    response_model=SubtractWithImageReferenceProblem,
)
def create_subtract_with_image_reference_problem(
    request: SubtractWithImageReferenceRequest,
) -> dict:
    try:
        return subtract_with_image_reference(
            object_name=request.object_name,
            initial_quantity=request.initial_quantity,
            removed_quantity=request.removed_quantity,
            figure=request.figure.model_dump(),
            actor_name=request.actor_name,
        )
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.post(
    "/join_corresponding_sums",
    response_model=JoinCorrespondingSumsProblem,
)
def create_join_corresponding_sums_problem(
    request: JoinCorrespondingSumsRequest,
) -> dict:
    try:
        return join_corresponding_sums(
            pair_count=request.pair_count,
            min_sum=request.min_sum,
            max_sum=request.max_sum,
            min_addend=request.min_addend,
            max_addend=request.max_addend,
            seed=request.seed,
        )
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.post(
    "/compose_and_decompose_numbers",
    response_model=ComposeAndDecomposeNumbersProblem,
)
def create_compose_and_decompose_numbers_problem(
    request: ComposeAndDecomposeNumbersRequest,
) -> dict:
    try:
        return compose_and_decompose_numbers(
            picture=request.picture.model_dump(),
            whole=request.whole,
            randomize_rows=request.randomize_rows,
            seed=request.seed,
        )
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.post(
    "/join_pictures_with_quantity",
    response_model=JoinPicturesWithQuantityProblem,
)
def create_join_pictures_with_quantity_problem(
    request: JoinPicturesWithQuantityRequest,
) -> dict:
    try:
        return join_pictures_with_quantity(
            available_pictures=[
                picture.model_dump() for picture in request.available_pictures
            ],
            container_count_per_side=request.container_count_per_side,
            min_quantity=request.min_quantity,
            max_quantity=request.max_quantity,
            seed=request.seed,
        )
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.post(
    "/sum_with_image_reference",
    response_model=SumWithImageReferenceProblem,
)
def create_sum_with_image_reference_problem(
    request: SumWithImageReferenceRequest,
) -> dict:
    try:
        return sum_with_image_reference(
            first_quantity=request.first_quantity,
            second_quantity=request.second_quantity,
            first_description=request.first_description,
            second_description=request.second_description,
            common_object=request.common_object,
            first_picture=request.first_picture.model_dump(),
            second_picture=request.second_picture.model_dump(),
            option_count=request.option_count,
            min_option_quantity=request.min_option_quantity,
            max_option_quantity=request.max_option_quantity,
            seed=request.seed,
        )
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@router.post(
    "/where_are_more_items",
    response_model=WhereAreMoreItemsProblem,
)
def create_where_are_more_items_problem(
    request: WhereAreMoreItemsRequest,
) -> dict:
    try:
        return where_are_more_items(
            available_pictures=[
                picture.model_dump() for picture in request.available_pictures
            ],
            comparison_count=request.comparison_count,
            min_quantity=request.min_quantity,
            max_quantity=request.max_quantity,
            seed=request.seed,
        )
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc
