from __future__ import annotations

from app.core import config


def test_alias_resolution(monkeypatch):
    monkeypatch.delenv("FIREBASE__FIREBASE_PROJECT_ID", raising=False)
    monkeypatch.delenv("VERTEX__MODEL", raising=False)
    monkeypatch.delenv("VERTEX__PROJECT_ID", raising=False)
    monkeypatch.delenv("VERTEX__LOCATION", raising=False)
    monkeypatch.delenv("DLP__PROJECT_ID", raising=False)

    monkeypatch.setenv("FIREBASE__PROJECT_ID", "alias-project")
    monkeypatch.setenv("VERTEX__MODEL", "publishers/google/models/test-model")
    monkeypatch.setenv("VERTEX__PROJECT_ID", "vertex-project")
    monkeypatch.setenv("VERTEX__LOCATION", "europe-west4")
    monkeypatch.setenv("DLP__PROJECT_ID", "dlp-alias")
    config.get_settings.cache_clear()

    settings = config.GatewaySettings()

    assert settings.firebase.project_id == "alias-project"
    assert settings.vertex_ai.model == "publishers/google/models/test-model"
    assert settings.vertex_ai.project_id == "vertex-project"
    assert settings.vertex_ai.location == "europe-west4"
    assert settings.dlp.project_id == "dlp-alias"


def test_allowed_origins_parsing(monkeypatch):
    monkeypatch.setenv("ALLOWED_ORIGINS", "https://a.com, https://b.com ,, https://c.com")
    config.get_settings.cache_clear()

    settings = config.GatewaySettings()

    assert settings.allowed_origins == [
        "https://a.com",
        "https://b.com",
        "https://c.com",
    ]


def test_dlp_project_fallback(monkeypatch):
    monkeypatch.setenv("FIREBASE__FIREBASE_PROJECT_ID", "fallback-project")
    monkeypatch.delenv("DLP__PROJECT_ID", raising=False)
    config.get_settings.cache_clear()

    settings = config.GatewaySettings()

    assert settings.dlp.project_id == "fallback-project"
