from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.comic import IssueWork
from src.db.session import SessionDep
from src.schema.comics.issuework import IssueWorkResponse, issuework_to_response


router = APIRouter()

@router.get("/issueworks", response_model=List[IssueWorkResponse])
def get_issueworks(db: SessionDep) -> List[IssueWorkResponse]:
    results: List[IssueWorkResponse] = []
    worktypes = db.query(IssueWork).all()
    for worktype in worktypes:
        response = issuework_to_response(worktype)
        results.append(response)
    return results

@router.get("/issueworks/{issuework_id}", response_model=IssueWorkResponse)
def get_issuework(issuework_id: str, db: SessionDep) -> IssueWorkResponse:
    worktype = db.get(IssueWork, issuework_id)
    if worktype is None:
        raise HTTPException(status_code=404, detail="Issuework could not be found")
    response = issuework_to_response(worktype)
    return response
