use crate::models::users::UserID;
use crate::schema::*;
use diesel::prelude::*;

#[derive(
    Debug, Clone, Copy, serde::Serialize, serde::Deserialize, PartialEq, Eq, Hash, PartialOrd, Ord,
)]
pub struct FileID(pub i32);

#[derive(Queryable, Debug, serde::Serialize, serde::Deserialize)]
pub struct File {
    id: i32,
    pub time_create: i64,
    pub mime_type: String,
    pub sha512: String,
    pub file_size: i32,
    pub file_name: String,
    user_id: i32,
}

impl File {
    pub fn id(&self) -> FileID {
        FileID(self.id)
    }
    pub fn file_path(&self) -> String {
        format!("blob/{}/{}", self.user_id, self.sha512)
    }

    pub fn user_id(&self) -> UserID {
        UserID(self.user_id)
    }
}

#[derive(Insertable)]
#[diesel(table_name = files)]
pub struct NewFile<'a> {
    pub time_create: i64,
    pub mime_type: &'a str,
    pub sha512: &'a str,
    pub file_size: i32,
    pub file_name: &'a str,
    pub user_id: i32,
}

impl NewFile<'_> {
    pub fn file_path(&self) -> String {
        format!("blob/{}/{}", self.user_id, self.sha512)
    }
}
