from fastapi import APIRouter, status, HTTPException
from sqlalchemy import select
from src.core.log_conf import logger
from src.apis.utils import SessionDep
from src.db.repository.media import create_new_mediaactor
from src.schema.media.actor import Actor, MediaActorResponse, get_actor_details
from src.db.models.media import MediaActor

router = APIRouter()

@router.get("/actors", response_model=list[MediaActorResponse])
# def get_all_files(db: SessionDep, review: bool = False, download: bool = False, current_user: Profile = Depends(get_current_user_from_token)) -> List[MediaFileResponse]:
def get_all_actors(db: SessionDep, review: bool = False, download: bool = False) -> list[MediaActorResponse]:
    results: list[MediaActorResponse] = []
    actors = db.scalars(select(MediaActor)).all()
    for mediaactor in actors:
        response = MediaActorResponse(id=mediaactor.id, name=str(mediaactor.name), url=str(mediaactor.url))
        results.append(response)
    return results

@router.get("/actors/{actor_id}", response_model=MediaActorResponse)
def get_actor(actor_id: str, db: SessionDep) -> MediaActorResponse:
    media_actor = db.get(MediaActor, actor_id)
    if not media_actor:
        raise HTTPException(status_code=404, detail="MediaActor could not be found")
    response = get_actor_details(media_actor)
    return response

@router.post("/actors", status_code=status.HTTP_201_CREATED)
def add_actor(new_actor: Actor, db: SessionDep) -> MediaActorResponse:
    logger.info(f"add actor {new_actor.url}")
    try:
        mediaActor: MediaActor = create_new_mediaactor(new_actor, db)
    except:
        raise HTTPException(status_code=409, detail="Link duplicate")
    response = get_actor_details(mediaActor)
    return response
