"""
Integration tests for Settings module
Tests configuration management, environment variables, and FastAPI integration

Auto-generated by RapidKit - Settings Module
"""

import os
import importlib.util

import pytest


def test_imports():
    """Test that all essential imports work"""
    from src.modules.free.essentials.settings import (
        Settings,
        get_settings,
        settings,
        settings_dependency,
        configure_fastapi_app,
    )

    assert Settings is not None
    assert get_settings is not None
    assert settings is not None
    assert settings_dependency is not None
    assert configure_fastapi_app is not None


def test_settings_singleton():
    """Test that get_settings returns same instance"""
    from src.modules.free.essentials.settings import get_settings

    settings1 = get_settings()
    settings2 = get_settings()

    assert settings1 is settings2


def test_settings_has_required_fields():
    """Test that Settings has essential configuration fields"""
    from src.modules.free.essentials.settings import settings

    # Core fields that should exist
    assert hasattr(settings, "PROJECT_NAME")
    assert hasattr(settings, "VERSION")
    assert hasattr(settings, "ENV")
    assert hasattr(settings, "DEBUG")

    # Verify types
    assert isinstance(settings.PROJECT_NAME, str)
    assert isinstance(settings.VERSION, str)
    assert isinstance(settings.ENV, str)
    assert isinstance(settings.DEBUG, bool)


def test_settings_environment_parsing():
    """Test environment variable parsing"""
    from src.modules.free.essentials.settings import Settings

    # Settings should be able to read from environment
    original_env = os.environ.get("DEBUG")

    try:
        os.environ["DEBUG"] = "true"
        # Create new instance to test env parsing
        test_settings = Settings()
        assert test_settings.DEBUG is True

        os.environ["DEBUG"] = "false"
        test_settings = Settings()
        assert test_settings.DEBUG is False

    finally:
        if original_env is not None:
            os.environ["DEBUG"] = original_env
        elif "DEBUG" in os.environ:
            del os.environ["DEBUG"]


def test_settings_field_validators():
    """Test that Field validators work"""
    from src.modules.free.essentials.settings import Settings

    settings = Settings()

    # PROJECT_NAME should not be empty
    assert len(settings.PROJECT_NAME) > 0

    # VERSION should follow semantic versioning pattern (basic check)
    assert "." in settings.VERSION or settings.VERSION.lower() == "dev"


def test_settings_dependency_function():
    """Test settings_dependency returns correct type"""
    from src.modules.free.essentials.settings.settings import settings_dependency, Settings

    result = settings_dependency()
    assert isinstance(result, Settings)


def test_settings_module_exports():
    """Test that all expected symbols are exported"""
    from importlib import import_module

    settings_module = import_module("src.modules.free.essentials.settings.settings")

    expected_exports = [
        "Settings",
        "get_settings",
        "settings",
        "settings_dependency",
        "Field",
        "BaseSettings",
    ]

    for export in expected_exports:
        assert hasattr(settings_module, export), f"Missing export: {export}"


def test_configure_fastapi_app_signature():
    """Test configure_fastapi_app has correct signature"""
    from src.modules.free.essentials.settings.settings import configure_fastapi_app
    import inspect

    sig = inspect.signature(configure_fastapi_app)
    params = list(sig.parameters.keys())

    assert "app" in params
    assert callable(configure_fastapi_app)


def test_settings_caching():
    """Test that settings are cached properly"""
    from src.modules.free.essentials.settings.settings import get_settings

    # Multiple calls should return cached instance
    s1 = get_settings()
    s2 = get_settings()
    s3 = get_settings()

    assert s1 is s2 is s3


def test_settings_validation():
    """Test settings validation on instantiation"""
    from src.modules.free.essentials.settings.settings import Settings

    # Should not raise validation errors with defaults
    try:
        settings = Settings()
        assert settings is not None
    except Exception as e:
        pytest.fail(f"Settings validation failed: {e}")


def test_custom_config_source():
    """Test CustomConfigSource is available"""
    from src.modules.free.essentials.settings import CustomConfigSource

    assert CustomConfigSource is not None
    assert callable(CustomConfigSource)


@pytest.mark.skipif(
    importlib.util.find_spec("fastapi") is None,
    reason="FastAPI not installed",
)
def test_fastapi_integration():
    """Test FastAPI integration if FastAPI is available"""
    try:
        from fastapi import FastAPI
        from src.modules.free.essentials.settings import configure_fastapi_app, Settings

        app = FastAPI()
        result = configure_fastapi_app(app)

        assert isinstance(result, Settings)
        assert hasattr(app.state, "settings")

    except ImportError:
        pytest.skip("FastAPI not available")
