from pathlib import Path

from django.utils.translation import gettext_lazy as _
from loadcredential import Credentials

credentials = Credentials(env_prefix="ERNESTOPHONE_")

BASE_DIR = Path(__file__).resolve().parent.parent

DEBUG = credentials.get_json("DEBUG", False)

ALLOWED_HOSTS = credentials.get_json("ALLOWED_HOSTS", [])

SECRET_KEY = credentials["SECRET_KEY"]

ADMINS = credentials.get_json("ADMINS", [])

SERVER_EMAIL = credentials.get("SERVER_EMAIL", "ernesto@localhost")


ACCOUNT_CREATION_PASS = credentials.get("ACCOUNT_CREATION_PASS", "dummy")


INSTALLED_APPS = [
    "propositions",
    "trombonoscope",
    "actu",
    "colorful",
    "pads",
    "calendrier",
    "gestion.apps.GestionConfig",
    "partitions.apps.PartitionsConfig",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "avatar",
    "instruments",
]

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",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "django.middleware.locale.LocaleMiddleware",
]

ROOT_URLCONF = "Ernestophone.urls"

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",
            ],
            "libraries": {
                "changelang": "gestion.templatetags.changelang",
                "modulo": "gestion.templatetags.modulo",
                "autotranslate": "gestion.templatetags.autotranslate",
                "halflength": "gestion.templatetags.halflength",
            },
        },
    }
]

WSGI_APPLICATION = "Ernestophone.wsgi.application"


###
# Database configuration
#   -> https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

DATABASES = credentials.get_json(
    "DATABASES",
    {
        "default": {
            "ENGINE": "django.db.backends.sqlite3",
            "NAME": BASE_DIR / "db.sqlite3",
        }
    },
)

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]


# I18n
# I18n
LANGUAGE_CODE = "fr"
LANGUAGES = (
    ("fr", _("Français")),
    ("en", _("English")),
)
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True
USE_TZ = True


LOCALE_PATHS = (BASE_DIR / "locale",)


AUTH_PROFILE_MODEL = "gestion.ErnestoUser"

LOGIN_URL = "/login"

AVATAR_CLEANUP_DELETED = True
AVATAR_AUTO_GENERATE_SIZES = (250,)
AVATAR_CHANGE_TEMPLATE = "trombonoscope/change_avatar.html"
AVATAR_MAX_AVATARS_PER_USER = 1
AVATAR_EXPOSE_USERNAMES = False
AVATAR_STORAGE_DIR = "trombonoscope"
AVATAR_ADD_TEMPLATE = "trombonoscope/add_avatar.html"
AVATAR_DELETE_TEMPLATE = "trombonoscope/delete_avatar.html"
AVATAR_DEFAULT_URL = "Ernesto"
AVATAR_PROVIDERS = (
    "avatar.providers.PrimaryAvatarProvider",
    "avatar.providers.DefaultAvatarProvider",
)
AVATAR_THUMB_FORMAT = "JPEG"

###
# Staticfiles configuration

STATIC_ROOT = credentials["STATIC_ROOT"]
STATIC_URL = "/static/"

MEDIA_ROOT = credentials.get("MEDIA_ROOT", BASE_DIR / "media")
MEDIA_URL = "/media/"

if DEBUG:
    EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

    INSTALLED_APPS += ["debug_toolbar"]
    MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware"] + MIDDLEWARE
