from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.comic import Volume
from src.db.session import SessionDep
from src.schema.comics.volume import VolumeResponse, volume_to_response

router = APIRouter()

@router.get("/volumes", response_model=List[VolumeResponse])
def volumes(db: SessionDep) -> List[VolumeResponse]:
    results: List[VolumeResponse] = []
    worktypes = db.query(Volume).all()
    for worktype in worktypes:
        response = volume_to_response(worktype)
        results.append(response)
    return results

@router.get("/volumes/{volume_id}", response_model=VolumeResponse)
def get_volume(volume_id: str, db: SessionDep) -> VolumeResponse:
    worktype = db.get(Volume, volume_id)
    if worktype is None:
        raise HTTPException(status_code=404, detail="Volume could not be found")
    response = volume_to_response(worktype)
    return response
