from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.comic import WorkType
from src.db.session import SessionDep
from src.schema.comics.worktype import WorktypeResponse, worktype_to_response


router = APIRouter()

@router.get("/worktypes", response_model=List[WorktypeResponse])
def get_issues(db: SessionDep) -> List[WorktypeResponse]:
    results: List[WorktypeResponse] = []
    worktypes = db.query(WorkType).all()
    for worktype in worktypes:
        response = worktype_to_response(worktype)
        results.append(response)
    return results

@router.get("/worktypes/{worktype_id}", response_model=WorktypeResponse)
def get_issue(worktype_id: str, db: SessionDep) -> WorktypeResponse:
    worktype = db.get(WorkType, worktype_id)
    if worktype is None:
        raise HTTPException(status_code=404, detail="Worktype could not be found")
    response = worktype_to_response(worktype)
    return response
