"""
Django settings for doc_generator project.

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

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

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

from pathlib import Path
import os
from django.urls import reverse_lazy

# 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/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-qb$!mc5&7qcph*kb&j@x$$zce*i9-3es0%9lw$x89n1nl6ncjy'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = [".rancher.lab", "127.0.0.1", "localhost"]


CSRF_TRUSTED_ORIGINS = ["http://*.rancher.lab", "http://trgen.rancher.lab:30343", "http://localhost:30343", "http://127.0.0.1"]


# Application definition

INSTALLED_APPS = [
    'filebrowser',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    
    'tinymce',  # For rich text editing
    'textdocs',
    'siemensusers',
]

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',
    'whitenoise.middleware.WhiteNoiseMiddleware',  # For serving static files in production
]

ROOT_URLCONF = 'doc_generator.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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',
            ],
        },
    },
]

WSGI_APPLICATION = 'doc_generator.wsgi.application'


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

if DEBUG == "True":
    DATABASES = { 
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': BASE_DIR / 'db.sqlite3',
        }
    } 
else:
    DATABASES = {
        'default': {
            "ENGINE": "django.db.backends.postgresql",
            "NAME": "trgenerator",
            "USER": "creylopez",
            "PASSWORD": "Dsa-0213",
            "HOST": os.environ.get('SQL_HOST','postgres-cluster-rw.siemens-pg-cluster.svc.cluster.local'),
            "PORT": "5432",
        }
    }

# Password validation
# https://docs.djangoproject.com/en/4.2/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',
    },
]

LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = 'document_list'
LOGOUT_REDIRECT_URL = 'document_list'

AUTH_USER_MODEL = "siemensusers.SiemensUser"

# Internationalization
# https://docs.djangoproject.com/en/4.2/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/4.2/howto/static-files/

STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

#STATICFILES_DIRS = [BASE_DIR / "static",]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'  # For production static file handling

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

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
#


# --- Add these settings for handling user-uploaded media files ---
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# --- Add this setting for the filebrowser ---
# This will create an 'uploads' folder inside your 'media' folder
FILE_BROWSER_DIRECTORY = 'uploads/'

# --- NOW, LET'S UPDATE THE TINYMCE CONFIG ---
TINYMCE_DEFAULT_CONFIG = {
    'height': 360,
    'width': 1120,
    'menubar': 'file edit view insert format tools table help',
    'plugins': 'advlist autolink lists link image charmap print preview anchor '
               'searchreplace visualblocks code fullscreen insertdatetime media table '
               'paste code help wordcount',
    'toolbar': 'undo redo | bold italic underline strikethrough | fontselect fontsizeselect formatselect | '
               'alignleft aligncenter alignright alignjustify | outdent indent |  '
               'numlist bullist | forecolor backcolor removeformat | pagebreak | '
               'charmap emoticons | fullscreen  preview save print | insertfile image media template link anchor codesample | '
               'ltr rtl',
    'custom_undo_redo_levels': 10,
    
    # --- This is the crucial part for connecting TinyMCE to the FileBrowser ---
    'file_picker_callback': 'filebrowser_callback',
    'file_picker_types': 'image', # We are only enabling it for images
}
