import os

from .settings import *


# Test environment should be deterministic and isolated from remote services.
DEBUG = False

# Local Postgres defaults align with docker-compose.test.yml.
DATABASES["default"] = {
    "ENGINE": os.environ.get("TEST_DB_ENGINE", "django.db.backends.postgresql"),
    "NAME": os.environ.get("TEST_DB_NAME", os.environ.get("POSTGRES_DB", "rad")),
    "USER": os.environ.get("TEST_DB_USER", os.environ.get("POSTGRES_USER", "django")),
    "PASSWORD": os.environ.get("TEST_DB_PASSWORD", os.environ.get("POSTGRES_PASSWORD", "postgres")),
    "HOST": os.environ.get("TEST_DB_HOST", "127.0.0.1"),
    "PORT": os.environ.get("TEST_DB_PORT", "5432"),
    "TEST": {
        "NAME": os.environ.get("TEST_DB_TEST_NAME", "test_rad"),
    },
}

# Keep tests fast and avoid external side effects.
EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
CACHES["default"] = {
    "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
}

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.immediate.ImmediateBackend",
        "QUEUES": ["default"],
    }
}

# Local media root for tests to avoid container-only paths in settings_local
MEDIA_ROOT = os.path.join(BASE_DIR, "media_test") + "/"
os.makedirs(MEDIA_ROOT, exist_ok=True)

# Remove debug_toolbar from installed apps and middleware to avoid NoReverseMatch errors when settings.DEBUG is False
if "debug_toolbar" in INSTALLED_APPS:
    INSTALLED_APPS.remove("debug_toolbar")
if "debug_toolbar.middleware.DebugToolbarMiddleware" in MIDDLEWARE:
    MIDDLEWARE.remove("debug_toolbar.middleware.DebugToolbarMiddleware")


