from typing import List, Type, AnyStr

from sqlalchemy.orm import Session

from src.core.log_conf import logger
from src.db.models.comic import Comic, Issue
from src.schema.comics.comic import ComicSchema
from src.schema.comics.issue import IssueDetailsResponse


def list_comics(db: Session) -> List[Type[Comic]]:
    comics = db.query(Comic).all()
    return comics


def get_issue_details(issue: Issue) -> IssueDetailsResponse:
    response = IssueDetailsResponse(
        id=issue.id,
        issue_number=issue.issue_number,
        in_stock=issue.in_stock,
        is_read=issue.is_read,
        comic_id=issue.comic_id,
        volume_id=issue.volume_id
    )
    return response


def update_comic(comic: ComicSchema, comic_id: AnyStr, db: Session) -> type[Comic] | None:
    logger.info(f"update_comic: {comic} with {comic_id}")
    comic = db.get(Comic, comic_id)
    return comic
