"""
Django settings for smartestate project.

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

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

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
from pathlib import Path
from environs import env
from environs.exceptions import EnvValidationError


env.read_env()

VERSION = '0.3.0'
COPYRIGHT_TEXT = """
Powered by <a href="https://github.com/saint-hilaire/smartestate">SmartEstate v{}</a>
(C) Brian St. Hilaire 2022 - 2025
""".format(VERSION)

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY', 'insecure-local-dev-key')

# SECURITY WARNING: don't run with debug turned on in production!
try:
    DEBUG = env.bool('DEBUG', False)
except EnvValidationError:
    DEBUG = False

ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', ['localhost', '127.0.0.1'])
CSRF_TRUSTED_ORIGINS = env.list('CSRF_TRUSTED_ORIGINS',
        ['http://localhost:8000', 'http://127.0.0.1:8000'])
USE_X_FORWARDED_HOST = True
SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False


# Application definition

INSTALLED_APPS = [
    'config.apps.ConfigConfig',
    'listings.apps.ListingsConfig',
    'broker.apps.BrokerConfig',
    'pages.apps.PagesConfig',
    'rest.apps.RestConfig',
    'django.contrib.admin',
    # If you want to use the admin integration, modeltranslation must be put
    # before django.contrib.admin (only applies when using Django 1.7
    # or above).
    'modeltranslation',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'cookie_consent',
    'rest_framework',
    'widget_tweaks',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
     # TODO: whitenoise is added as a workaround for serving
     # staticfiles. Once the infrastructure is improved,
     # it needs to be removed again.
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'smartestate.urls'

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_ROOT, 'templates').replace('\\', '/')
        ],
        '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',
                'django.template.context_processors.request',
                'config.context_processors.site_title',
                'config.context_processors.logo_url',
                'config.context_processors.theme',
                'config.context_processors.show_filter_search_on_homepage',
                'config.context_processors.show_filter_search_in_listview',
                'pages.context_processors.all_pages',
                'broker.context_processors.listing_search_form',
                'smartestate.context_processors.languages',
                'smartestate.context_processors.current_language',
                'smartestate.context_processors.smartestate_version',
                'smartestate.context_processors.copyright_text',
            ],
        },
    },
]

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

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)


WSGI_APPLICATION = 'smartestate.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': env('DATABASE_ENGINE', 'django.db.backends.sqlite3'),
        'NAME': env('DATABASE_NAME', BASE_DIR / 'db.sqlite3'),
        'USER': env('DATABASE_USER', None),
        'PASSWORD': env('DATABASE_PASSWORD', None),
        'HOST': env('DATABASE_HOST', '127.0.0.1'),
        'PORT': env('DATABASE_PORT', 3306),
    }
}


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

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'rest.utils.se_exception_handler',
}



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

LANGUAGE_CODE = env('LANGUAGE_CODE', 'en-us')

TIME_ZONE = env('TIME_ZONE', 'UTC')

USE_I18N = True

USE_TZ = True

# The gettext lambda function is not a feature of modeltranslation, but rather
# required for Django to be able to (statically) translate the verbose names
# of the languages using the standard i18n solution.
gettext = lambda s: s
LANGUAGES = (
    ('en', gettext('English')),
    ('de', gettext('German')),
    ('fr', gettext('French')),
    ('es', gettext('Spanish')),
    ('it', gettext('Italian')),
)


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

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

COOKIE_CONSENT_NAME = "cookie_consent"
