pub mod convert;
pub mod decode;
pub mod dump;
pub mod encode;
pub mod import;
pub mod list;
pub mod r#match;
pub mod migrate;
pub mod push;
pub mod put;
pub mod settings;
pub mod validate;

pub const PIDBOX_URL: &str = "https://metadata.vraix.org/pidbox.sqlite3.zst";
pub const PIDBOX_CACHE_KEY: &str = "pidbox.sqlite3.zst";
pub const VRAIX_CACHE_TTL: std::time::Duration =
    std::time::Duration::from_secs(30 * 24 * 60 * 60);

/// DataCite annual public data file base URL (2025 edition, 108 M records, 33 GB gzip).
/// Actual download requires a time-limited JWT token obtained from the page below.
/// Users obtain a token by submitting their email at:
///   https://datafiles.datacite.org/datafiles/public-2025
/// and pass the full download URL as the positional `input` argument:
///   commonmeta import "https://datafiles.datacite.org/datafiles/public-2025/download?token=<JWT>"
pub const DATACITE_ANNUAL_HOST: &str = "datafiles.datacite.org";

/// Resolve the path to the local commonmeta works SQLite database.
///
/// Precedence (highest first):
///   1. `explicit` — the value of a `--file` CLI flag when provided
///   2. `COMMONMETA_DB` environment variable
///   3. Platform default:
///      - macOS  → `~/Library/Application Support/commonmeta/commonmeta.sqlite3`
///      - Linux  → `/var/lib/commonmeta/commonmeta.sqlite3`
///      - other  → `./commonmeta.sqlite3`
pub fn resolve_db_path(explicit: Option<&String>) -> String {
    if let Some(p) = explicit {
        return p.clone();
    }
    if let Ok(p) = std::env::var("COMMONMETA_DB") {
        return p;
    }
    platform_default_db_path()
}

/// Resolve the path to the commonmeta cache SQLite database.
///
/// Precedence (highest first):
///   1. `explicit` — the value of a `--cache-db` CLI flag when provided
///   2. `CACHE_DB` environment variable
///   3. Platform default:
///      - Linux  → `/var/lib/commonmeta/cache.sqlite3`
///      - macOS  → `~/Library/Application Support/commonmeta/cache.sqlite3`
///      - other  → `./cache.sqlite3`
pub fn resolve_cache_db_path(explicit: Option<&String>) -> String {
    if let Some(p) = explicit {
        return p.clone();
    }
    if let Ok(p) = std::env::var("CACHE_DB") {
        return p;
    }
    platform_default_cache_db_path()
}

fn platform_default_cache_db_path() -> String {
    #[cfg(target_os = "linux")]
    {
        "/var/lib/commonmeta/cache.sqlite3".to_string()
    }
    #[cfg(target_os = "macos")]
    {
        let home = std::env::var("HOME").unwrap_or_default();
        format!(
            "{}/Library/Application Support/commonmeta/cache.sqlite3",
            home
        )
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        "cache.sqlite3".to_string()
    }
}

fn platform_default_db_path() -> String {
    #[cfg(target_os = "macos")]
    {
        let home = std::env::var("HOME").unwrap_or_default();
        format!(
            "{}/Library/Application Support/commonmeta/commonmeta.sqlite3",
            home
        )
    }
    #[cfg(target_os = "linux")]
    {
        "/var/lib/commonmeta/commonmeta.sqlite3".to_string()
    }
    #[cfg(not(any(target_os = "macos", target_os = "linux")))]
    {
        "commonmeta.sqlite3".to_string()
    }
}

