use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Websocket {
    pub host: String,
    pub port: i16,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Credentials {
    pub user: String,
    pub password: String,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Endpoint {
    pub host: String,
    pub port: i16,
    pub database: String,
    pub credentials: Credentials,
}

/// Database backend selection.
///
/// YAML examples:
/// ```yaml
/// # SQLite (recommended for new installs)
/// database: !sqlite /mendia/data/mendia.db
///
/// # MySQL / MariaDB (existing installs)
/// database: !mysql
///   host: db
///   port: 3306
///   database: mendia
///   credentials:
///     user: root
///     password: example
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DatabaseConfig {
    #[cfg(feature = "mysql")]
    Mysql(Endpoint),
    Sqlite(String),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TheMovieDb {
    pub api_key: String,
    pub language: String,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct Notifications {
    /// ISO 639-1 (or 2-letter) language codes treated as "primary" in notification messages.
    /// e.g. `["en", "de"]` → "has English and German audio".
    /// If empty, all detected languages are listed.
    ///
    /// YAML example:
    /// ```yaml
    /// notifications:
    ///   main_languages:
    ///     - en
    ///     - de
    /// ```
    #[serde(default)]
    pub main_languages: Vec<String>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Settings {
    pub database: DatabaseConfig,
    pub websocket: Websocket,
    /// Optional Telegram bot notifications. Omit section to disable.
    pub telegram: Option<Telegram>,
    /// Optional `WhatsApp` bot notifications. Omit section to disable.
    pub whatsapp: Option<WhatsApp>,
    pub tmdb: TheMovieDb,
    /// Notification formatting options.
    #[serde(default)]
    pub notifications: Notifications,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Telegram {
    pub token: String,
    pub chatid: String,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct WhatsApp {
    /// Path to the `SQLite` file used to persist the `WhatsApp` session.
    pub db_path: String,
    /// `JID` to send notifications to.
    /// Individual: `"15551234567@s.whatsapp.net"`
    /// Group: `"1234567890-1234567890@g.us"`
    pub jid: String,
}
