from app.config import Settings


def test_settings_load_core_fields_from_env(monkeypatch) -> None:
    monkeypatch.setenv("APP_DATABASE_URL", "sqlite:///./data/app.db")
    monkeypatch.setenv("APP_HOSTNAME", "home.example.com")
    monkeypatch.setenv("POO_WEBHOOK_ID", "poo-hook")
    monkeypatch.setenv("POO_SENSOR_ENTITY_NAME", "sensor.test_poo_status")
    monkeypatch.setenv("POO_SENSOR_FRIENDLY_NAME", "Poo Status")
    monkeypatch.setenv("HOME_ASSISTANT_BASE_URL", "http://ha.local:8123")
    monkeypatch.setenv("HOME_ASSISTANT_AUTH_TOKEN", "token")
    monkeypatch.setenv("HOME_ASSISTANT_TIMEOUT_SECONDS", "2.5")
    monkeypatch.setenv("AUTH_BOOTSTRAP_USERNAME", "admin")
    monkeypatch.setenv("AUTH_BOOTSTRAP_PASSWORD", "secret")
    monkeypatch.setenv("AUTH_SESSION_COOKIE_NAME", "auth_cookie")
    monkeypatch.setenv("AUTH_SESSION_TTL_HOURS", "8")
    monkeypatch.setenv("APP_ENV", "production")

    settings = Settings(_env_file=None)

    assert settings.app_database_url == "sqlite:///./data/app.db"
    assert settings.poo_webhook_id == "poo-hook"
    assert settings.poo_sensor_entity_name == "sensor.test_poo_status"
    assert settings.poo_sensor_friendly_name == "Poo Status"
    assert settings.home_assistant_base_url == "http://ha.local:8123"
    assert settings.home_assistant_auth_token == "token"
    assert settings.home_assistant_timeout_seconds == 2.5
    assert settings.app_hostname == "home.example.com"
    assert settings.app_base_url == "https://home.example.com"
    assert settings.ticktick_redirect_uri == "https://home.example.com/ticktick/auth/code"
    assert settings.auth_bootstrap_username == "admin"
    assert settings.auth_bootstrap_password == "secret"
    assert settings.auth_session_cookie_name == "auth_cookie"
    assert settings.auth_session_ttl_hours == 8
    assert settings.app_sqlite_path is not None
    assert settings.app_sqlite_path.name == "app.db"
    assert settings.auth_cookie_secure is True


def test_settings_derive_development_ticktick_redirect_uri(monkeypatch) -> None:
    monkeypatch.setenv("APP_ENV", "development")
    monkeypatch.setenv("APP_HOSTNAME", "localhost:11001")

    settings = Settings(_env_file=None)

    assert settings.app_base_url == "http://localhost:11001"
    assert settings.ticktick_redirect_uri == "http://localhost:11001/ticktick/auth/code"


def test_settings_support_smtp_fields(monkeypatch) -> None:
    monkeypatch.setenv("SMTP_ENABLED", "true")
    monkeypatch.setenv("SMTP_HOST", "smtp.example.com")
    monkeypatch.setenv("SMTP_PORT", "2525")
    monkeypatch.setenv("SMTP_USERNAME", "smtp-user")
    monkeypatch.setenv("SMTP_PASSWORD", "smtp-password")
    monkeypatch.setenv("SMTP_FROM_NAME", "Home Automation")
    monkeypatch.setenv("SMTP_FROM_ADDRESS", "sender@example.com")
    monkeypatch.setenv("SMTP_TO_ADDRESS", "recipient@example.com")
    monkeypatch.setenv("SMTP_USE_STARTTLS", "false")

    settings = Settings(_env_file=None)

    assert settings.smtp_enabled is True
    assert settings.smtp_host == "smtp.example.com"
    assert settings.smtp_port == 2525
    assert settings.smtp_username == "smtp-user"
    assert settings.smtp_password == "smtp-password"
    assert settings.smtp_from_name == "Home Automation"
    assert settings.smtp_from_address == "sender@example.com"
    assert settings.smtp_to_address == "recipient@example.com"
    assert settings.smtp_use_starttls is False
