"""
Django settings for marinerg_tap project.

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

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

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

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/

secret_key_env = os.environ.get("DJANGO_SECRET_KEY")
if not secret_key_env:
    raise RuntimeError("No DJANGO_SECRET_KEY key set in environment")
SECRET_KEY = secret_key_env.replace('"', "")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(os.environ.get("DJANGO_DEBUG", default=0))

ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", default="*").split(",")

ADMIN_URL = os.environ.get("DJANGO_ADMIN_URL", default="admin")

API_AUTH_URL = os.environ.get("DJANGO_API_AUTH_URL", default="api-auth")

# OIDC Connection
WITH_OIDC = bool(os.environ.get("WITH_OIDC", default=0))
OIDC_RP_CLIENT_ID = os.environ.get("OIDC_RP_CLIENT_ID")
OIDC_RP_CLIENT_SECRET = os.environ.get("OIDC_RP_CLIENT_SECRET")
OIDC_RP_SIGN_ALGO = "RS256"
OIDC_OP_AUTHORIZATION_ENDPOINT = os.environ.get("OIDC_OP_AUTHORIZATION_ENDPOINT")
OIDC_OP_TOKEN_ENDPOINT = os.environ.get("OIDC_OP_TOKEN_ENDPOINT")
OIDC_OP_USER_ENDPOINT = os.environ.get("OIDC_OP_USER_ENDPOINT")
OIDC_OP_JWKS_ENDPOINT = os.environ.get("OIDC_OP_JWKS_ENDPOINT")

# Application definition
INSTALLED_APPS = [
    "ichec_django_core.apps.IchecDjangoCoreConfig",
    "rest_framework",
    "corsheaders",
    "django_countries",
    "rest_framework.authtoken",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]

if WITH_OIDC:
    INSTALLED_APPS.append("mozilla_django_oidc")
    LOGIN_REDIRECT_URL = "/"
    LOGOUT_REDIRECT_URL = "/"

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "corsheaders.middleware.CorsMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

if WITH_OIDC:
    MIDDLEWARE.append("mozilla_django_oidc.middleware.SessionRefresh")


# Auth
AUTHENTICATION_BACKENDS = ["django.contrib.auth.backends.ModelBackend"]
if WITH_OIDC:
    AUTHENTICATION_BACKENDS = [
        "mozilla_django_oidc.auth.OIDCAuthenticationBackend"
    ] + AUTHENTICATION_BACKENDS

ROOT_URLCONF = "app.urls"


def get_templates(base_dir: Path):
    return [
        {
            "BACKEND": "django.template.backends.django.DjangoTemplates",
            "DIRS": [os.path.join(base_dir, "templates")],
            "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",
                ],
            },
        },
    ]


TEMPLATES = get_templates(BASE_DIR)

WSGI_APPLICATION = "app.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases


def get_databases(base_dir: Path):
    return {
        "default": {
            "ENGINE": os.environ.get("DJANGO_SQL_ENGINE", "django.db.backends.sqlite3"),
            "NAME": os.environ.get("DJANGO_SQL_DATABASE", base_dir / "db.sqlite3"),
            "USER": os.environ.get("DJANGO_SQL_USER", "user"),
            "PASSWORD": os.environ.get("DJANGO_SQL_PASSWORD", "password"),
            "HOST": os.environ.get("DJANGO_SQL_HOST", "localhost"),
            "PORT": os.environ.get("DJANGO_SQL_PORT", "5432"),
        }
    }


DATABASES = get_databases(BASE_DIR)


# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

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


# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True

SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"


def get_static_root(base_dir: Path):
    return Path(os.environ.get("DJANGO_STATIC_ROOT", default=base_dir / "staticfiles"))


STATIC_ROOT = get_static_root(BASE_DIR)


def get_media_root(base_dir: Path):
    return Path(os.environ.get("DJANGO_MEDIA_ROOT", default=base_dir / "mediafiles"))


MEDIA_ROOT = get_media_root(BASE_DIR)

DEFAULT_FILE_STORAGE = "django_downloadview.storage.SignedStorage"
DOWNLOADVIEW_BACKEND = "django_downloadview.nginx.XAccelRedirectMiddleware"

DOWNLOADVIEW_RULES = [
    {
        "source_url": "/media/nginx/",
        "destination_url": "/nginx-optimized-by-middleware/",
    },
]

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

if WITH_OIDC:
    DRF_DEFAULT_AUTH = "mozilla_django_oidc.contrib.drf.OIDCAuthentication"
else:
    DRF_DEFAULT_AUTH = "rest_framework.authentication.TokenAuthentication"

REST_FRAMEWORK = {
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
    "DEFAULT_AUTHENTICATION_CLASSES": [
        DRF_DEFAULT_AUTH,
        "rest_framework.authentication.SessionAuthentication",
    ],
    "DEFAULT_PAGINATION_CLASS": "ichec_django_core.view_sets.pagination.PageNumberWithSizePagination",  # NOQA
    "PAGE_SIZE": 100,
}

CORS_ALLOWED_ORIGINS = [
    "http://localhost:4200",
]

if False:
    EMAIL_HOST_USER = "Test User"
    EMAIL_BACKEND = "django.core.mail.backends.filebased.EmailBackend"
    EMAIL_FILE_PATH = os.getcwd() + "/app-messages"
else:
    EMAIL_HOST = os.environ.get("DJANGO_EMAIL_HOST", default="")
    EMAIL_PORT = os.environ.get("DJANGO_EMAIL_PORT", default="")
    EMAIL_HOST_USER = os.environ.get("DJANGO_EMAIL_HOST_USER", default="")
    EMAIL_HOST_PASSWORD = os.environ.get("DJANGO_EMAIL_HOST_PASSWORD", default="")
    EMAIL_USE_TLS = True

LOGGING = {
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
        },
    },
    "root": {
        "handlers": ["console"],
        "level": "DEBUG",
    },
}
