from __future__ import annotations

from pytest import MonkeyPatch

from core.config.settings import Settings


def test_runtime_settings_defaults() -> None:
    settings = Settings()

    assert settings.runtime.log_json is True
    assert settings.runtime.log_rotation == "time"
    assert settings.runtime.log_rotation_when == "midnight"
    assert settings.runtime.log_rotation_interval == 1
    assert settings.runtime.log_rotation_backup_count == 14
    assert settings.runtime.log_rotation_max_bytes == 10_000_000
    assert settings.runtime.log_dir == "logs"
    assert settings.runtime.log_error_dir == "logs/errors"
    assert settings.runtime.log_file_name == "app.log"
    assert settings.runtime.log_error_file_name == "app.error.log"
    assert "password" in settings.runtime.log_sensitive_fields


def test_runtime_settings_env_override(monkeypatch: MonkeyPatch) -> None:
    monkeypatch.setenv("SOCIAL_RUNTIME__LOG_DIR", "var/logs")
    monkeypatch.setenv("SOCIAL_RUNTIME__LOG_ERROR_DIR", "var/logs/errors")
    monkeypatch.setenv("SOCIAL_RUNTIME__LOG_FILE_NAME", "custom.log")
    monkeypatch.setenv("SOCIAL_RUNTIME__LOG_ERROR_FILE_NAME", "custom-error.log")
    monkeypatch.setenv("SOCIAL_RUNTIME__LOG_ROTATION", "size")
    monkeypatch.setenv("SOCIAL_RUNTIME__LOG_ROTATION_MAX_BYTES", "2048")

    settings = Settings()

    assert settings.runtime.log_dir == "logs"
    assert settings.runtime.log_error_dir == "logs/errors"
    assert settings.runtime.log_file_name == "custom.log"
    assert settings.runtime.log_error_file_name == "custom-error.log"
    assert settings.runtime.log_rotation == "size"
    assert settings.runtime.log_rotation_max_bytes == 2048


def test_runtime_settings_default_file_names_follow_service_name(
    monkeypatch: MonkeyPatch,
) -> None:
    monkeypatch.delenv("SOCIAL_RUNTIME__LOG_FILE_NAME", raising=False)
    monkeypatch.delenv("SOCIAL_RUNTIME__LOG_ERROR_FILE_NAME", raising=False)
    monkeypatch.setenv("SOCIAL_RUNTIME__SERVICE_NAME", "worker-default")

    settings = Settings()

    assert settings.runtime.log_dir == "logs"
    assert settings.runtime.log_error_dir == "logs/errors"
    assert settings.runtime.log_file_name == "worker-default.log"
    assert settings.runtime.log_error_file_name == "worker-default.error.log"
