use std::path::PathBuf;

use crate::util::hash::Hash;
use chrono::NaiveDateTime;
use diesel::Queryable;
use serde::Serialize;

table! {
    files (id) {
        id -> Integer,
        hash -> Binary,
        path -> Text,
        valid -> Bool,
        added -> Timestamp,
        size -> BigInt,
        mtime -> Nullable<Timestamp>,
    }
}

#[derive(Queryable, Serialize, Clone, Debug)]
pub struct File {
    pub id: i32,
    pub hash: Hash,
    pub path: String,
    pub valid: bool,
    pub added: NaiveDateTime,
    pub size: i64,
    pub mtime: Option<NaiveDateTime>,
}

// todo - remove, try_from the actual model, impl queryable...
#[derive(Serialize, Clone, Debug)]
pub struct OutFile {
    pub id: i32,
    pub hash: Hash,
    pub path: PathBuf,
    pub valid: bool,
    pub added: NaiveDateTime,
    pub size: i64,
    pub mtime: Option<NaiveDateTime>,
}

#[derive(Insertable, Debug)]
#[table_name = "files"]
pub struct NewFile {
    pub hash: Vec<u8>,
    pub path: String,
    pub added: NaiveDateTime,
    pub size: i64,
    pub mtime: Option<NaiveDateTime>,
}

impl From<File> for crate::addressing::Address {
    fn from(file: File) -> Self {
        crate::addressing::Address::Hash(file.hash)
    }
}
