"""
Django Settings for Storm Logger 2.0 Testing
Sophisticated Django test environment
"""

import os
from pathlib import Path

# Build paths
BASE_DIR = Path(__file__).resolve().parent.parent.parent

# Security settings
SECRET_KEY = "storm-logger-test-secret-key-for-django-hooks-testing-only"
DEBUG = True
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "testserver"]

# Application definition
INSTALLED_APPS = [
    "django.contrib.contenttypes",
    "django.contrib.auth",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.admin",
    "tests.django_hooks.test_app",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "tests.django_hooks.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

# Database
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": ":memory:",
    }
}

# Internationalization
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True

# Static files
STATIC_URL = "/static/"

# Default primary key field type
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Storm Logger Configuration for Testing
from storm_logger import DjangoStormLoggerConfig, LogLevel

# Test environment configuration
storm_config = DjangoStormLoggerConfig(
    log_level=LogLevel.DEBUG,
    enable_test_logging=True,
    enable_business_metrics=True,
    track_user_actions=True,
    track_performance_metrics=True,
    enable_django_db_logging=True,
    test_log_file="django_test_logs.log",
    log_directory=BASE_DIR / "logs" / "tests",
    enable_console=False,  # Disable console output during tests
)

# Django logging configuration
LOGGING = storm_config.for_django_settings()

# Test-specific overrides
if "pytest" in os.environ.get("_", ""):
    # Override for pytest
    LOGGING["handlers"]["console"]["level"] = "CRITICAL"
    LOGGING["loggers"][""]["level"] = "DEBUG"

# Environment variable overrides
if os.environ.get("STORM_LOGGER_TEST_MODE"):
    storm_config.enable_console = False
    storm_config.log_directory = Path(os.environ.get("STORM_LOGGER_LOG_DIR", BASE_DIR / "logs" / "tests"))

# Cache configuration for testing
CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
        "LOCATION": "storm-logger-test-cache",
    }
}

# Test-specific Django settings
if "test" in os.environ.get("DJANGO_SETTINGS_MODULE", ""):
    # Speed up tests
    PASSWORD_HASHERS = [
        "django.contrib.auth.hashers.MD5PasswordHasher",
    ]
    
    # Disable migrations for testing
    MIGRATION_MODULES = {
        "auth": None,
        "contenttypes": None,
        "sessions": None,
        "test_app": None,
    }