"""
Django settings for test environment.

Only set BACKEND_URL and FRONTEND_URL in your .env:
  BACKEND_URL=http://localhost:8001
  FRONTEND_URL=http://localhost:3001

All CORS, ALLOWED_HOSTS, etc. are derived automatically from base.py.
"""
import os

from .base import *  # noqa: F401, F403

DEBUG = True

# Override secret key for tests (deterministic)
SECRET_KEY = 'django-insecure-test-key-for-testing-only'

# Allow testserver (used by Django test client)
ALLOWED_HOSTS += ['testserver']  # noqa: F405

# Database — use a separate test database
DATABASES['default']['NAME'] = os.getenv('DATABASE_NAME', 'nexa_test')  # noqa: F405
DATABASES['default']['PORT'] = os.getenv('DATABASE_PORT', '5433')  # noqa: F405

# Disable password validators for faster tests
AUTH_PASSWORD_VALIDATORS = []

# CORS — allow all in tests for convenience
CORS_ALLOW_ALL_ORIGINS = True

# Email backend for testing
EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'

# Celery — run tasks synchronously in tests
CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_EAGER_PROPAGATES = True
CELERY_BROKER_URL = 'memory://'

# Logging — quieter in tests
LOGGING['root']['level'] = 'WARNING'  # noqa: F405

# Cache — use in-memory cache for tests (no Redis dependency)
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'test-cache',
    }
}

# REST Framework — use JSON for test requests
REST_FRAMEWORK['TEST_REQUEST_DEFAULT_FORMAT'] = 'json'  # noqa: F405

# django-tenants — Allow requests even when domain is not found
# (test transactions are not visible to middleware domain lookups)
SHOW_PUBLIC_IF_NO_TENANT_FOUND = True

# Replace TenantMainMiddleware with test-only middleware
# that resolves tenant within the test transaction
MIDDLEWARE = [
    m if m != 'django_tenants.middleware.main.TenantMainMiddleware'
    else 'apps.core.tenant.test_middleware.TestTenantMiddleware'
    for m in MIDDLEWARE  # noqa: F405
]
