"""
Production settings for Rapha-Bethel BNPS
InMotion Hosting — cPanel / Passenger WSGI
Domain: https://raphabethel.org/portal
"""
import os
from pathlib import Path
from decouple import config, Csv

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

# This tells Django all URLs are prefixed with /portal
FORCE_SCRIPT_NAME = '/portal'

SECRET_KEY = config('SECRET_KEY')
# DEBUG = False
DEBUG = False
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='raphabethel.org,www.raphabethel.org', cast=Csv())

INSTALLED_APPS = [
    'jazzmin',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
    'students',
    'staff',
    'academics',
    'finance',
    'communication',
    'parents',
    'reports',
    'api',
    'sync',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',   # serves static files in production
    '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',
]

ROOT_URLCONF = 'backend.urls'

TEMPLATES = [
    {
        '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',
                'core.context_processors.permissions',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'backend.wsgi.application'

# ── MySQL (InMotion cPanel database) ────────────────────────────
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME':     config('DB_NAME',     default='dnjill5_bnps'),
        'USER':     config('DB_USER',     default='dnjill5_bnps'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST':     config('DB_HOST',     default='localhost'),
        'PORT':     config('DB_PORT',     default='3306'),
        'OPTIONS': {
            'charset': 'utf8mb4',
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

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

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Africa/Douala'
USE_I18N = True
USE_TZ = True

# ── Static & Media ───────────────────────────────────────────────
STATIC_URL  = '/portal/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

# This prevents the "MissingFileError" during collectstatic
WHITENOISE_MANIFEST_STRICT = False

MEDIA_URL  = '/portal/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
AUTH_USER_MODEL = 'core.CustomUser'

LOGIN_URL             = 'staff:login'
LOGIN_REDIRECT_URL    = 'staff:teacher_dashboard'
LOGOUT_REDIRECT_URL   = 'home'

# ── Security (HTTPS) ────────────────────────────────────────────
SECURE_PROXY_SSL_HEADER      = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT          = True
SESSION_COOKIE_SECURE        = True
CSRF_COOKIE_SECURE           = True
SECURE_HSTS_SECONDS          = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_BROWSER_XSS_FILTER    = True
X_FRAME_OPTIONS              = 'SAMEORIGIN'   # needed for Django admin iframes

# ── Jazzmin ─────────────────────────────────────────────────────
JAZZMIN_SETTINGS = {
    "site_title":    "Rapha-Bethel BNPS",
    "site_header":   "Rapha-Bethel",
    "site_brand":    "Rapha-Bethel BNPS",
    "site_logo":     "admin/img/logo-yellow.jpeg",
    "login_logo":    "admin/img/Rapha-Bethel-Logo1.png",
    "welcome_sign":  "Welcome to Rapha-Bethel BNPS School Management",
    "copyright":     "Vi-2s-Dk Foundation",
    "search_model":  ["students.Student", "academics.SchoolClass"],
    "topmenu_links": [
        {"name": "Dashboard",    "url": "admin:index", "permissions": ["auth.view_user"]},
        {"name": "Staff Portal", "url": "/portal", "new_window": False},
        {"name": "School Website", "url": "https://RaphaBethel.org/BNPS", "new_window": True},
    ],
    "show_sidebar": True,
     # 1. Define the custom link
    "custom_links": {
        "External": [{
            "name": "Staff Portal",
            "url": "https://raphabethel.org/portal",
            "icon": "fas fa-school-link-alt",
            "new_window": True,  # Opens in a new tab
        }, 
        {
            "name": "School Website",
            "url": "https://raphabethel.org/BNPS",
            "icon": "fas fa-globe-link-alt",
            "new_window": True,  # Opens in a new tab
        }]
        
    },
    "navigation_expanded": True,
    "icons": {
        "auth":                     "fas fa-users-cog",
        "auth.user":                "fas fa-user",
        "auth.Group":               "fas fa-users",
        "students.Student":         "fas fa-user-graduate",
        "students.ParentGuardian":  "fas fa-user-friends",
        "academics.SchoolClass":    "fas fa-chalkboard-teacher",
        "academics.Attendance":     "fas fa-clipboard-check",
        "finance.FeeStructure":     "fas fa-money-bill-wave",
        "finance.Invoice":          "fas fa-file-invoice",
        "communication.Announcement": "fas fa-bullhorn",
    },
    "default_icon_parents":  "fas fa-chevron-circle-right",
    "default_icon_children": "fas fa-circle",
}