"""
Django settings for myproject project.

Generated by 'django-admin startproject' using Django 6.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/6.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/6.0/ref/settings/
"""

import os
from pathlib import Path

# 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/6.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
# On PythonAnywhere set DJANGO_SECRET_KEY in the WSGI file (see deploy notes).
SECRET_KEY = os.environ.get(
    'DJANGO_SECRET_KEY',
    'django-insecure-$a&6*&ke!q3mpfey@=-=ds77x%j^ole(gl6@h%(r1ukmawua!+',
)

# SECURITY WARNING: don't run with debug turned on in production!
# Defaults to True for local dev; set DJANGO_DEBUG=False on the server.
DEBUG = os.environ.get('DJANGO_DEBUG', 'True') == 'True'

# Comma-separated list of hosts, e.g. "yourdomain.com,www.yourdomain.com".
# Defaults to '*' so local dev works; set DJANGO_ALLOWED_HOSTS on the server.
ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', '*').split(',')


# Hosts allowed to submit POST forms over HTTPS (hosting + tunnels).
CSRF_TRUSTED_ORIGINS = [
    "https://*.pythonanywhere.com",
    "https://*.ngrok-free.app",
    "https://*.ngrok-free.dev",
    "https://*.ngrok.app",
    "https://*.ngrok.io",
]

# Add your cPanel domain here in production, e.g.
# DJANGO_CSRF_TRUSTED_ORIGINS="https://yourdomain.com,https://www.yourdomain.com"
_extra_csrf = os.environ.get('DJANGO_CSRF_TRUSTED_ORIGINS', '')
if _extra_csrf:
    CSRF_TRUSTED_ORIGINS += [o.strip() for o in _extra_csrf.split(',') if o.strip()]




# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
]

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/dashboard/'

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    # WhiteNoise serves collected static files directly from Django, so the
    # admin/app CSS works on cPanel without configuring an Apache alias.
    # Must come right after SecurityMiddleware.
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'main.middleware.LastSeenMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'myproject.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # Put the app's template dir on the filesystem loader (which runs before
        # the app-directories loader) so our admin overrides — base_site.html and
        # the dashboard index — take priority over the ones django.contrib.admin
        # ships. Without this they are shadowed, because 'main' is listed after
        # 'django.contrib.admin' in INSTALLED_APPS.
        'DIRS': [BASE_DIR / 'main' / 'templates'],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.request',
                'django.template.context_processors.i18n',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'myproject.wsgi.application'


# Database
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators

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',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/6.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/6.0/howto/static-files/

STATIC_URL = 'static/'
# `collectstatic` gathers admin/app static here; map /static/ -> this dir in the
# PythonAnywhere Web tab so the admin CSS loads when DEBUG is off.
STATIC_ROOT = BASE_DIR / 'staticfiles'

# Uploaded files (deposit QR codes and payment-proof screenshots).
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

# Compress collected static files (no manifest hashing, so a missing
# {% static %} reference can't 500 the whole site). WhiteNoise serves them.
STORAGES = {
    'default': {
        'BACKEND': 'django.core.files.storage.FileSystemStorage',
    },
    'staticfiles': {
        'BACKEND': 'whitenoise.storage.CompressedStaticFilesStorage',
    },
}

# Supported languages for the site. Covers the languages of the countries
# offered in the login / register phone-country selector (Czechia, Poland,
# Slovakia, Austria, Belgium, France, UK, Germany, Spain, Italy, Netherlands,
# US, China, India, Japan, South Korea, UAE).
LANGUAGES = [
    ('en', 'English'),
    ('de', 'Deutsch'),
    ('fr', 'Français'),
    ('it', 'Italiano'),
    ('nl', 'Nederlands'),
    ('cs', 'Čeština'),
    ('pl', 'Polski'),
    ('sk', 'Slovenčina'),
    ('es', 'Español'),
    ('zh-hans', '中文'),
    ('hi', 'हिन्दी'),
    ('ja', '日本語'),
    ('ko', '한국어'),
    ('ar', 'العربية'),
]

# Optional: where to store compiled translation files (you can also put locale/ inside apps)
LOCALE_PATHS = [BASE_DIR / 'locale']
