use clap::Parser;
use server::Config;

const CLEARED: [(&str, Option<&str>); 6] = [
    ("DATABASE_URL", None),
    ("BIND_ADDR", None),
    ("APP_NAME", None),
    ("SESSION_COOKIE_SECURE", None),
    ("DEFAULT_LANGUAGE", None),
    ("DEFAULT_TIMEZONE", None),
];

#[test]
fn parses_from_args_with_defaults() {
    temp_env::with_vars(CLEARED, || {
        let cfg = Config::try_parse_from(["server", "--database-url", "postgres://localhost/test"])
            .expect("should parse");

        assert_eq!(cfg.database_url, "postgres://localhost/test");
        assert_eq!(cfg.bind_addr, "0.0.0.0:8080");
        assert_eq!(cfg.app_name, "Collection Management System");
        assert_eq!(cfg.default_language, "sv");
        assert_eq!(cfg.default_timezone, "Europe/Stockholm");
    });
}

#[test]
fn database_url_is_required() {
    temp_env::with_vars(CLEARED, || {
        assert!(Config::try_parse_from(["server"]).is_err());
    });
}

#[test]
fn cookie_secure_defaults_to_true() {
    temp_env::with_vars(CLEARED, || {
        let config = Config::try_parse_from(["server", "--database-url", "postgres://x"]).unwrap();
        assert!(config.cookie_secure);
    });
}
