from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.comic import Issue
from src.db.session import SessionDep
from src.schema.comics.issue import IssueResponse, issue_to_response


router = APIRouter()

@router.get("/issues", response_model=List[IssueResponse])
def get_issues(db: SessionDep) -> List[IssueResponse]:
    results: List[IssueResponse] = []
    issues = db.query(Issue).all()
    for issue in issues:
        response = issue_to_response(issue)
        results.append(response)
    return results

@router.get("/issues/{issue_id}", response_model=IssueResponse)
def get_issue(issue_id: str, db: SessionDep) -> IssueResponse:
    issue = db.get(Issue, issue_id)
    if issue is None:
        raise HTTPException(status_code=404, detail="Issue could not be found")
    response = issue_to_response(issue)
    return response
