use magnetar_model::ck;
use magnetar_sdk::types::drive::{DriveFileBase, ImageMeta};
use serde::Deserialize;
use url::Url;

use crate::model::{PackType, PackingContext};

#[derive(Debug)]
pub struct PackFileBaseInput<'a> {
    pub file: &'a ck::drive_file::Model,
    pub effective_url: Option<&'a Url>,
    pub effective_thumbnail_url: Option<&'a Url>,
}

impl PackType<PackFileBaseInput<'_>> for DriveFileBase {
    fn extract(
        _context: &PackingContext,
        PackFileBaseInput {
            file,
            effective_url,
            effective_thumbnail_url,
        }: PackFileBaseInput<'_>,
    ) -> Self {
        let media_metadata = ImageMeta::deserialize(file.properties.clone()).unwrap_or_default();

        DriveFileBase {
            name: file.name.clone(),
            created_at: file.created_at.into(),
            size: file.size as usize,
            hash: None, // TODO: blake3
            mime_type: file.r#type.clone(),
            media_metadata,
            url: effective_url.map(Url::to_string),
            source_url: file.url.clone(),
            thumbnail_url: effective_thumbnail_url.map(Url::to_string),
            blurhash: file.blurhash.clone(),
            sensitive: file.is_sensitive,
            comment: file.comment.clone(),
            folder_id: file.folder_id.clone(),
            user_id: file.user_id.clone(),
        }
    }
}
