from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.comic import ComicWork
from src.db.session import SessionDep
from src.schema.comics.comicwork import ComicWorkResponse, comicwork_to_response


router = APIRouter()

@router.get("/comicworks", response_model=List[ComicWorkResponse])
def get_comicworks(db: SessionDep) -> List[ComicWorkResponse]:
    results: List[ComicWorkResponse] = []
    worktypes = db.query(ComicWork).all()
    for worktype in worktypes:
        response = comicwork_to_response(worktype)
        results.append(response)
    return results

@router.get("/comicworks/{comicwork_id}", response_model=ComicWorkResponse)
def get_comicwork(comicwork_id: str, db: SessionDep) -> ComicWorkResponse:
    worktype = db.get(ComicWork, comicwork_id)
    if worktype is None:
        raise HTTPException(status_code=404, detail="Comicwork could not be found")
    response = comicwork_to_response(worktype)
    return response
