from sqlalchemy.orm import Session
from typing import AnyStr
import uuid
from datetime import datetime
from src.core.log_conf import logger
from src.db.models.media import MediaFile, MediaVideo
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
    media_video.created_date = datetime.now()
    media_video.last_modified_date = datetime.now()
    media_video.review = True
    media_video.should_download = True
    db.add(media_video)
    db.commit()
    db.refresh(media_video)
    print(media_video)
    return media_video

def create_new_mediafile(link: AnyStr, db: Session) -> MediaFile:
    logger.info("create MediaFile with url {link}")
    media_file: MediaFile = MediaFile()
    media_file.id = str(uuid.uuid4())
    media_file.url = link
    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

