from sqlalchemy.orm import Session
import uuid
from datetime import datetime
from src.core.log_conf import logger
from src.db.models.media import MediaActor, MediaActorFile, MediaFile, MediaVideo
from src.schema.media.actor import Actor
from src.webapps.media.forms import AddLinkForm


def create_new_video(video: AddLinkForm, db: Session) -> MediaVideo:
    print(video.url)
    media_video = MediaVideo()
    media_video.id = str(uuid.uuid4())
    media_video.url = video.url # type: ignore
    media_video.created_date = datetime.now()
    media_video.last_modified_date = datetime.now()
    media_video.review = True # type: ignore
    media_video.should_download = True # type: ignore
    db.add(media_video)
    db.commit()
    db.refresh(media_video)
    print(media_video)
    return media_video

def create_new_mediafile(link: str, db: Session) -> MediaFile:
    logger.info("create MediaFile with url {link}")
    media_file: MediaFile = MediaFile()
    media_file.id = str(uuid.uuid4())
    media_file.url = link # type: ignore
    media_file.created_date = datetime.now()
    media_file.last_modified_date = datetime.now()
    media_file.version = 0
    media_file.review = True
    media_file.should_download = True
    db.add(media_file)
    db.commit()
    db.refresh(media_file)
    logger.info(f"created {media_file}")
    return media_file

def create_new_mediaactor(new_actor: Actor, db: Session) -> MediaActor:
    logger.info(f"create MediaActor with url {new_actor.url}")
    media_actor: MediaActor = MediaActor()
    media_actor.id = str(uuid.uuid4())
    media_actor.name = str(new_actor.name) # type: ignore
    media_actor.url = str(new_actor.url) # type: ignore
    media_actor.created_date = datetime.now()
    media_actor.last_modified_date = datetime.now()
    media_actor.version = 0
    db.add(media_actor)
    db.commit()
    db.refresh(media_actor)
    logger.info(f"created {media_actor}")
    return media_actor

def create_new_mediaactorfile(db: Session, actor_id: str, file_id: str) -> MediaActorFile:
    logger.info(f"create MediaActorFile with actor {actor_id} and file {file_id}")
    media_actor_file: MediaActorFile = MediaActorFile()
    media_actor_file.id = str(uuid.uuid4())
    media_actor_file.created_date = datetime.now()
    media_actor_file.last_modified_date = datetime.now()
    media_actor_file.version = 0
    media_actor_file.media_actor_id = actor_id # type: ignore
    media_actor_file.media_file_id = file_id # type: ignore
    db.add(media_actor_file)
    db.commit()
    db.refresh(media_actor_file)
    return media_actor_file

def delete_mediaactorfile(db: Session, actorfile_id: str):
    logger.info(f"delete MediaActorFile with id {actorfile_id}")
    media_actorfile = db.get(MediaActorFile, actorfile_id)
    db.delete(media_actorfile)
    db.commit()