"""
Django settings for school management system.
"""
import os
from pathlib import Path
from decouple import config, Csv

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

SECRET_KEY = config('SECRET_KEY', default='django-insecure-local-dev-only-do-not-use-in-production')
DEBUG = config('DEBUG', default=True, cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='127.0.0.1,localhost', cast=Csv())

# Application definition
INSTALLED_APPS = [
    'jazzmin',  # Must be before django.contrib.admin
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    # Local apps - Use FULL path from manage.py
    'core',
    'students',
    'staff',
    'academics',
    'finance',
    'communication',
    'parents',
    'reports',
    'api',
    'sync',
]

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

ROOT_URLCONF = 'backend.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),  # Add this line
        ],
        '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'

# Database
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Password validation
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
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  # For production
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),  # Your custom static directory
]

# Media files (Uploaded files)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Default primary key field type
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# Add at the bottom of the file
LOGIN_URL = 'staff:login'  # Where to redirect if not logged in
LOGIN_REDIRECT_URL = 'staff:teacher_dashboard'  # Where teachers go after login
LOGOUT_REDIRECT_URL = 'home'  # Where to go after logout


# 1. Update settings to use custom user model
# Add this to backend/settings.py:
AUTH_USER_MODEL = 'core.CustomUser'

# Jazzmin settings
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",
    "login_logo_css": "max-height: 20px; width: auto;", # Adjust height and width as needed
    "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": "/staff/dashboard/", "new_window": False},
        {"name": "School Website", "url": "https://RaphaBethel.org/BNPS", "new_window": True},
    ],
    "show_sidebar": True,
    "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",
}