from typing import List

from fastapi import APIRouter, HTTPException

from src.core.log_conf import logger
from src.db.models.comic import Comic
from src.db.repository.comics.comic import (
    get_comic_details,
)
from src.db.session import SessionDep
from src.schema.comics.comic import ComicResponse, comic_to_response
from src.schema.comics.comic_details import ComicDetailsResponse

router = APIRouter()


@router.get("/comics")
def get_all_comics(db: SessionDep) -> List[ComicResponse]:
    results: List[ComicResponse] = []
    comics = db.query(Comic).all()
    for comic in comics:
        response = comic_to_response(comic)
        results.append(response)
    return results


@router.get("/comics/{comic_id}", response_model=ComicResponse)
def get_comic(comic_id: str, db: SessionDep) -> ComicResponse:
    comic = db.get(Comic, comic_id)
    if comic is None:
        raise HTTPException(status_code=404, detail="Comic could not be found")
    response: ComicResponse = comic_to_response(comic)
    return response


@router.get("/comics/details/{comic_id}", response_model=ComicDetailsResponse)
def get_comic_details_by_id(comic_id: str, db: SessionDep) -> ComicDetailsResponse:
    comic = db.get(Comic, comic_id)
    if comic is None:
        raise HTTPException(status_code=404, detail="Comic could not be found")
    logger.info(f"create ComicDetailsResponse for {comic}")
    response: ComicDetailsResponse = get_comic_details(comic)
    logger.info(f"ComicDetailsResponse: {response}")
    return response
