use crate::{DateTimeUtc, UserId};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

/// Stored as `"daily"`, `"weekly"`, or `"never"` in the database.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum DigestMode {
    Daily,
    Weekly,
    Never,
}

impl std::fmt::Display for DigestMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DigestMode::Daily => write!(f, "daily"),
            DigestMode::Weekly => write!(f, "weekly"),
            DigestMode::Never => write!(f, "never"),
        }
    }
}

impl std::str::FromStr for DigestMode {
    type Err = &'static str;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "daily" => Ok(DigestMode::Daily),
            "weekly" => Ok(DigestMode::Weekly),
            "never" => Ok(DigestMode::Never),
            _ => Err("unknown digest mode"),
        }
    }
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "user_notification")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub user: UserId,
    pub email_enabled: bool,
    pub in_app_enabled: bool,
    pub push_enabled: bool,
    pub digest_mode: String,
    pub dnd_enabled: bool,
    pub dnd_start_minute: Option<i32>,
    pub dnd_end_minute: Option<i32>,
    pub marketing_enabled: bool,
    pub security_enabled: bool,
    pub product_enabled: bool,
    pub push_subscription_endpoint: Option<String>,
    pub push_subscription_keys_p256dh: Option<String>,
    pub push_subscription_keys_auth: Option<String>,
    pub created_at: DateTimeUtc,
    pub updated_at: DateTimeUtc,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
