from importlib import import_module
from pathlib import Path

from configtype.jsonconfig import setup_search_paths

BASE_DIR = Path(__file__).resolve().parents[5]
setup_search_paths("/etc/ocyan/", str(BASE_DIR))


def _patch_ocyan_test_bootstrap():
    # Keep the isolated Coyote test settings out of the broader Ocyan
    # block-plugin discovery chain. The smoke suite validates Coyote itself and
    # should not pull in optional settings blocks from unrelated plugins such
    # as newsletter.
    wagtail_block_plugin = import_module("ocyan.plugin.wagtail.block_plugin")
    wagtail_block_plugin.get_extra_ocyan_settings_blocks = lambda: []

    # The generic Ocyan class loader normally walks the full project extension
    # graph. For plugin-local smoke tests we only want the directly installed
    # base module for each request, not unrelated host overrides such as
    # webshop extensions.
    ocyan_loading = import_module("ocyan.main.loading")
    ocyan_loading.ocyan_class_loader.get_extension_module_names = (
        lambda oscar_app_name, installed_apps_entry: [installed_apps_entry]
    )


_patch_ocyan_test_bootstrap()

SECRET_KEY = "coyote-test-key"
DEBUG = True
USE_TZ = True
TIME_ZONE = "UTC"
ALLOWED_HOSTS = ["*"]
ROOT_URLCONF = "ocyan.plugin.coyote.test_urls"
STATIC_URL = "/static/"
STATIC_ROOT = str(BASE_DIR / "static")
COMPRESS_ROOT = STATIC_ROOT
SITE_ID = 1
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
WAGTAILADMIN_BASE_URL = "http://testserver"

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

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.messages",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.staticfiles",
    "compressor",
    "modelcluster",
    "taggit",
    "wagtail",
    "wagtail.admin",
    "wagtail.documents",
    "wagtail.embeds",
    "wagtail.images",
    "wagtail.search",
    "wagtail.sites",
    "wagtail.snippets",
    "wagtail.users",
    "wagtail.contrib.forms",
    "wagtail.contrib.redirects",
    "wagtail.contrib.settings",
    "wagtailmetadata",
    "roadrunner",
    "ocyan.main",
    "oxyan.themes",
    "ocyan.plugin.wagtail_i18n",
    "ocyan.plugin.wagtail",
    "ocyan.plugin.coyote",
]

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",
]

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",
            ],
        },
    }
]

PASSWORD_HASHERS = [
    "django.contrib.auth.hashers.MD5PasswordHasher",
]

OXYAN_THEME = "default"
OXYAN_THEME_MIXINS = []
OXYAN_HEADER_OPTIONS = []
OXYAN_LAZY_THEME_DEFINITIONS = "ocyan.plugin.coyote.definitions.get_coyote_definitions"

COMPRESS_ENABLED = False
