"""
Django settings for the integration test backend.

Provides:
- HTTP server functions (echo, add)
- WebSocket channels (chat, notifications, presence)
- JWT authentication
- Form integration (login, signup, add_email)
"""

import os

SECRET_KEY = "integration-test-secret-key-not-for-production"

DEBUG = True

ALLOWED_HOSTS = ["*"]

INSTALLED_APPS = [
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "mizan",
    "testapp",
]

AUTH_USER_MODEL = "testapp.EmailUser"

MIDDLEWARE = [
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
]

ROOT_URLCONF = "testapp.urls"

TEMPLATES = [
    {
        "BACKEND": "mizan.ssr.MizanTemplates",
        "DIRS": [os.path.join(os.path.dirname(__file__), "..", "..", "frontend")],
        "OPTIONS": {
            "worker": os.path.join(
                os.path.dirname(__file__), "..", "..", "..", "..",
                "packages", "mizan-ssr", "src", "worker.tsx",
            ),
        },
    },
]

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": os.path.join(os.path.dirname(__file__), "..", "db.sqlite3"),
    }
}

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

ASGI_APPLICATION = "testapp.asgi.application"

# JWT
JWT_PRIVATE_KEY = "integration-test-jwt-secret-key"
JWT_ALGORITHM = "HS256"

# Channel layers — Redis when available, in-memory fallback for local dev
REDIS_URL = os.environ.get("REDIS_URL", "")
if REDIS_URL:
    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels_redis.core.RedisChannelLayer",
            "CONFIG": {"hosts": [REDIS_URL]},
        },
    }
else:
    CHANNEL_LAYERS = {
        "default": {
            "BACKEND": "channels.layers.InMemoryChannelLayer",
        },
    }

# Session
SESSION_ENGINE = "django.contrib.sessions.backends.db"

# CORS — allow React dev server
CSRF_TRUSTED_ORIGINS = [
    "http://localhost:3000",
    "http://localhost:5173",
    "http://localhost:5174",
]
