from typing import List

from fastapi import APIRouter, HTTPException

from src.db.models.bookshelf import Author
from src.db.session import SessionDep
from src.schema.bookshelf.author import AuthorResponse, to_response

router = APIRouter()

@router.get("/authors", response_model=List[AuthorResponse])
def get_all_authors(db: SessionDep) -> List[AuthorResponse]:
    results: List[AuthorResponse] = []
    authors = db.query(Author).all()
    for author in authors:
        response = to_response(author)
        results.append(response)
    return results

@router.get("/authors/{author_id}", response_model=AuthorResponse)
def get_author(author_id: str, db: SessionDep) -> AuthorResponse:
    author = db.get(Author, author_id)
    if author is None:
        raise HTTPException(status_code=404, detail="Author could not be found")
    response = to_response(author)
    return response
