from fastapi import APIRouter, status, HTTPException
from sqlalchemy import select
from src.apis.utils import SessionDep
from src.db.models.media import MediaActorFile
from src.db.repository.media import delete_mediaactorfile
from src.schema.media.actorfile import MediaActorFileResponse, get_actorfile_details

router = APIRouter()

@router.get("/actorfiles", response_model=list[MediaActorFileResponse])
def get_all_actorfiles(db: SessionDep) -> list[MediaActorFileResponse]:
    results: list[MediaActorFileResponse] = []
    actorfiles = db.scalars(select(MediaActorFile)).all()
    for mediaactorfile in actorfiles:
        response = MediaActorFileResponse(id=mediaactorfile.id, actor_id=str(mediaactorfile.media_actor_id), file_id=str(mediaactorfile.media_file_id))
        results.append(response)
    return results

@router.get("/actorfiles/{actorfile_id}", response_model=MediaActorFileResponse)
def get_actorfile(actorfile_id: str, db: SessionDep) -> MediaActorFileResponse:
    media_actorfile = db.get(MediaActorFile, actorfile_id)
    if not media_actorfile:
        raise HTTPException(status_code=404, detail="MediaActor could not be found")
    response = get_actorfile_details(media_actorfile)
    return response

@router.delete("/actorfiles/{actorfile_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_actorfile(actorfile_id: str, db: SessionDep):
    media_actorfile = db.get(MediaActorFile, actorfile_id)
    if not media_actorfile:
        raise HTTPException(status_code=404, detail="MediaActor could not be found")
    delete_mediaactorfile(db, media_actorfile.id)

