from datetime import datetime
from typing import Optional

from src.db.models.media import MediaFile
from pydantic import BaseModel


class MediaFileResponse(BaseModel):
    id: str
    created_date: datetime
    last_modified_date: datetime
    version: int
    title: Optional[str]
    file_name: Optional[str]
    cloud_link: Optional[str]
    url: Optional[str]
    review: bool = False
    should_download: bool = False


def file_to_response(mediafile: MediaFile) -> MediaFileResponse:
    """
    Create MediaFileResponse from model.
    """
    response: MediaFileResponse = MediaFileResponse(
        id=mediafile.id,
        created_date=mediafile.created_date,
        last_modified_date=mediafile.last_modified_date,
        version=mediafile.version,
        title=mediafile.title,
        file_name=mediafile.file_name,
        cloud_link=mediafile.cloud_link,
        url=mediafile.url,
        review=mediafile.review,
        should_download=mediafile.should_download,
    )
    return response


def file_to_model(model: MediaFileResponse, mediafile: MediaFile) -> MediaFile:
    """
    Set data of response to model.
    """
    mediafile.file_name = model.file_name
    mediafile.cloud_link = model.cloud_link
    if model.url is not None:
        mediafile.url = model.url
    else:
        mediafile.url = ""
    if model.title is not None:
        mediafile.title = model.title
    else:
        mediafile.title = ""
    mediafile.last_modified_date = datetime.now()
    mediafile.review = model.review
    mediafile.should_download = model.should_download
    return mediafile


class MediaFileModel(BaseModel):
    """
    Pydantic model to import MediaFile.
    """
    id: str
    created_date: Optional[datetime]
    last_modified_date: Optional[datetime]
    version: int = 0
    title: Optional[str]
    file_name: Optional[str]
    cloud_link: Optional[str]
    url: Optional[str]
    review: bool = True
    should_download: bool = True
    

class Link(BaseModel):
    """
    PYdantic model for uploading url.
    """

    url: str
