"""
Django settings for src project.

Generated by 'django-admin startproject' using Django 5.2.6.

For more information on this file, see
https://docs.djangoproject.com/en/5.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.2/ref/settings/
"""

import base64
import decimal
import os
import sys
from pathlib import Path

from terminusgps.authorizenet.constants import Environment, ValidationMode
from terminusgps.wialon.flags import TokenFlag

ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
BASE_DIR = Path(__file__).resolve().parent.parent
LOGIN_REDIRECT_URL = "/dashboard/"
LOGIN_URL = "/login/"
DEBUG = True
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
DJANGO_ENCRYPTED_FIELD_ALGORITHM = os.getenv(
    "DJANGO_ENCRYPTED_FIELD_ALGORITHM", "SS20"
)
DJANGO_ENCRYPTED_FIELD_KEY = base64.b64decode(
    os.getenv("DJANGO_ENCRYPTED_FIELD_KEY", "")
)
LANGUAGE_CODE = "en-us"
MERCHANT_AUTH_ENVIRONMENT = Environment.SANDBOX
MERCHANT_AUTH_LOGIN_ID = os.getenv("MERCHANT_AUTH_LOGIN_ID")
MERCHANT_AUTH_TRANSACTION_KEY = os.getenv("MERCHANT_AUTH_TRANSACTION_KEY")
MERCHANT_AUTH_VALIDATION_MODE = ValidationMode.TEST
ROOT_URLCONF = "src.urls"
SECRET_KEY = (
    "django-insecure-o63#9m5p-j-&o2ub897i9r+1#)81j8%e06-%-6u1ktsdw+*%+*"
)
STATIC_URL = "static/"
TIME_ZONE = "US/Central"
USE_I18N = False
USE_TZ = True
WIALON_RESOURCE_NAME = "Terminus GPS Notifications"
WIALON_TOKEN = os.getenv("WIALON_TOKEN")
WIALON_TOKEN_ACCESS_TYPE = (
    TokenFlag.VIEW_ACCESS
    | TokenFlag.MANAGE_NONSENSITIVE
    | TokenFlag.MANAGE_SENSITIVE
)
WSGI_APPLICATION = "src.wsgi.application"
NOTIFICATIONS_SETUP_FEE = decimal.Decimal("140.00")
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

ADMINS = (
    ("Peter", "pspeckman@terminusgps.com"),
    ("Blake", "blake@terminusgps.com"),
    ("Lili", "lili@terminusgps.com"),
)

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False if "test" not in sys.argv else True,
    "formatters": {
        "verbose": {
            "format": "%(asctime)s [%(process)d] [%(module)s] [%(levelname)s] %(message)s",
            "datefmt": "[%Y-%m-%d %H:%M:%S%z]",
            "class": "logging.Formatter",
        },
        "simple": {
            "format": "%(asctime)s [%(levelname)s] %(message)s",
            "datefmt": "[%Y-%m-%d %H:%M:%S%z]",
            "class": "logging.Formatter",
        },
    },
    "handlers": {
        "console": {"class": "logging.StreamHandler", "formatter": "simple"},
        "console_verbose": {
            "class": "logging.StreamHandler",
            "formatter": "verbose",
        },
    },
    "root": {"handlers": ["console"], "level": "DEBUG"},
    "loggers": {
        "django": {
            "handlers": ["console"],
            "level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
            "propagate": False,
        },
        "terminusgps_notifications": {
            "handlers": ["console_verbose"],
            "level": os.getenv("NOTIFICATIONS_LOG_LEVEL", "DEBUG"),
            "propagate": False,
        },
        "terminusgps_payments": {
            "handlers": ["console_verbose"],
            "level": os.getenv("PAYMENTS_LOG_LEVEL", "DEBUG"),
            "propagate": False,
        },
        "authorizenet.sdk": {
            "handlers": ["console"],
            "level": os.getenv("AUTHORIZENET_LOG_LEVEL", "DEBUG"),
            "propagate": False,
        },
    },
}

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django_rq",
    "django_tasks",
    "terminusgps_payments.apps.TerminusgpsPaymentsConfig",
    "terminusgps_notifications.apps.TerminusgpsNotificationsConfig",
]

RQ_QUEUES = {"default": {"HOST": "localhost", "PORT": 6379, "DB": 0}}

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.rq.RQBackend",
        "QUEUES": ["default"],
    }
}

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379",
    }
}

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

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ]
        },
    }
]

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