import os
from tempfile import mktemp

import django

try:
    from psycopg2cffi import compat

    compat.register()
except ImportError:
    pass

DEBUG = True
STATIC_URL = "/static/"

SITE_ID = 1
ROOT_URLCONF = "demo.urls"
SECRET_KEY = "abc"
STATIC_ROOT = mktemp("static")
MEDIA_ROOT = mktemp("media")

INSTALLED_APPS = [
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.sites",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.contrib.admin",
    "concurrency",
    "reversion",
    "demo",
]
IS_DJANGO_5 = django.VERSION[0] == 5
if IS_DJANGO_5:
    MIGRATION_MODULES = {
        "demo": "demo.migrations5",
        "auth": "demo.auth_migrations5",
    }
else:
    MIGRATION_MODULES = {
        "demo": "demo.migrations",
        "auth": "demo.auth_migrations",
    }

MIDDLEWARE_CLASSES = []
MIDDLEWARE = [
    # 'concurrency.middleware.ConcurrencyMiddleware',
    "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",
]

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.contrib.auth.context_processors.auth",
                "django.template.context_processors.debug",
                "django.template.context_processors.i18n",
                "django.template.context_processors.media",
                "django.template.context_processors.static",
                "django.template.context_processors.tz",
                "django.contrib.messages.context_processors.messages",
                "django.template.context_processors.request",
            ],
            # ... some options here ...
        },
    },
]

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "formatters": {
        "full": {"format": "%(levelname)-8s: %(asctime)s %(module)s %(process)d %(thread)d %(message)s"},
        "verbose": {"format": "%(levelname)-8s: %(asctime)s %(name)-25s %(message)s"},
        "simple": {"format": "%(levelname)-8s %(asctime)s %(name)-25s %(funcName)s %(message)s"},
        "debug": {"format": "%(levelno)s:%(levelname)-8s %(name)s %(funcName)s:%(lineno)s:: %(message)s"},
    },
    "filters": {"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}},
    "handlers": {
        "null": {"level": "DEBUG", "class": "logging.NullHandler"},
        "console": {"level": "DEBUG", "class": "logging.StreamHandler", "formatter": "debug"},
    },
    "loggers": {"concurrency": {"handlers": ["null"], "propagate": False, "level": "DEBUG"}},
}

db = os.environ.get("DBENGINE", "pg")
dbname = os.environ.get("DBNAME", "concurrency")
if db == "pg":
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql_psycopg2",
            "NAME": dbname,
            "HOST": os.environ.get("PGHOST", "127.0.0.1"),
            "PORT": os.environ.get("PGPORT", "5432"),
            "USER": "postgres",
            "PASSWORD": "postgres",
        }
    }
elif db == "mysql":
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.mysql",
            "NAME": dbname,
            "HOST": "127.0.0.1",
            "PORT": "",
            "USER": "root",
            "PASSWORD": "root",
            "CHARSET": "utf8",
            "COLLATION": "utf8_general_ci",
        }
    }
else:
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": dbname,
        }
    }

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

AUTHENTICATION_BACKENDS = (
    "demo.backends.AnyUserAuthBackend",
    # os.environ.get("AUTHENTICATION_BACKEND", ""),
    # "django.contrib.auth.backends.ModelBackend",
)
