from pathlib import Path

import pytest
from pydantic import ValidationError

from zbt.core.config import BACKEND_ROOT, Settings, resolve_env_file


def test_settings_load_environment_from_backend_directory() -> None:
    assert Settings.model_config["env_file"] == BACKEND_ROOT / ".env"


def test_resolve_env_file_from_project_root(tmp_path: Path) -> None:
    backend = tmp_path / "backend"
    backend.mkdir()
    env_file = backend / ".env"
    env_file.write_text("MYSQL_USER=root\n", encoding="utf-8")

    assert resolve_env_file(tmp_path) == env_file
    assert resolve_env_file(backend) == env_file


@pytest.mark.parametrize(
    ("field", "value"),
    [
        ("mysql_core_database", "insurance_core"),
        ("mysql_agent_database", "insurance_agent"),
        ("mysql_analytics_database", "insurance_analytics"),
        ("redis_prefix", "ins:"),
    ],
)
def test_stage_one_configuration_rejects_unsafe_data_targets(
    field: str,
    value: str,
) -> None:
    with pytest.raises(ValidationError):
        Settings(
            app_env="test",
            jwt_access_secret="a" * 32,
            jwt_refresh_secret="b" * 32,
            field_encryption_key="c" * 32,
            **{field: value},
        )
