use std::collections::HashMap;
use serde::{Deserialize,Serialize, Deserializer};

// use crate::defs::AppDBs;
use crate::users::User;

fn default_empty() -> String {
  "".to_string()
}
fn default_items() -> HashMap<String,String> {
  HashMap::new()
}
fn default_expire() -> u64 {
   300
}
fn default_send_email() -> bool {
  false  
}
fn default_isadmin() -> bool {
  false  
}
fn default_otp_empty() -> String {
  String::from("")
}
fn deserialize_id<'de, D>(deserializer: D) -> Result<String, D::Error>
    where D: Deserializer<'de> {
    Ok(String::deserialize(deserializer)?)
}

#[derive(Default,Deserialize,Serialize,Debug,Clone)]
pub struct AllUserData {
    pub user: User,
    pub openids: String,
}
#[derive(Default,Deserialize,Serialize,Debug,Clone)]
pub struct UserData {
    #[serde(default = "default_empty",deserialize_with = "deserialize_id")]
    pub id: String, 
    #[serde(default = "default_empty")]
    pub name: String, 
    #[serde(default = "default_empty")]
    pub fullname: String, 
    #[serde(default = "default_empty")]
    pub description: String, 
    #[serde(default = "default_empty")]
    pub email: String, 
    #[serde(default = "default_empty")]
    pub password: String, 
    #[serde(default = "default_otp_empty")]
    pub otp_code: String,
    #[serde(default = "default_otp_empty")]
    pub otp_url: String,
    #[serde(default = "default_otp_empty")]
    pub otp_auth: String,
    #[serde(default = "default_empty")]
    pub roles: String, 
    #[serde(default = "default_items")]
    pub items: HashMap<String,String>,
    #[serde(default = "default_empty")]
    pub opendis: String,
}
// impl UserData {
//     pub fn from_id(id: String, _app_dbs: &AppDBs) -> Self {
//       Self { 
//           id,
//           name: String::from(""),
//           fullname: String::from(""),
//           description: String::from(""),
//           email: String::from(""),
//           password: String::from(""),
//           roles: String::from(""),
//           items: Vec::new()
//       }
//     }
// //     pub fn contents_to_json(&self) -> Vec<String> {
// //         self.items.clone().into_iter().map(|item| item.to_json()).collect()
// //     }
//}
#[derive(Default,Deserialize,Serialize,Debug,Clone)]
pub struct UserLogin {
    #[serde(default = "default_empty")]
    pub name: String, 
    #[serde(default = "default_empty")]
    pub password: String, 
    #[serde(default = "default_empty")]
    pub otp_auth: String,
    #[serde(default = "default_empty")]
    pub email: String,
}

#[derive(Default,Deserialize,Serialize,Debug,Clone)]
pub struct UserItem {
    #[serde(default = "default_empty")]
    pub name: String, 
    #[serde(default = "default_empty")]
    pub value: String, 
}

#[derive(Default,Deserialize,Serialize,Debug,Clone)]
pub struct UserInvitation {
    #[serde(default = "default_empty")]
    pub email: String, 
    #[serde(default = "default_empty")]
    pub roles: String, 
    #[serde(default = "default_expire")]
    pub expire: u64, 
    #[serde(default = "default_send_email")]
    pub send_email: bool,
    #[serde(default = "default_isadmin")]
    pub isadmin: bool,
}
