"""
Django settings will use prefix of DJANGO_ for environment variables.
"""

import os
from pathlib import Path
import sys
from dotenv import load_dotenv

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

load_dotenv(dotenv_path = BASE_DIR / '.env')

FRONT_DIR = os.getenv('DJANGO_FRONT_DIR', BASE_DIR / 'front')
MODEL_DIR = os.getenv('DJANGO_MODEL_DIR', BASE_DIR / 'model')

SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')
DEBUG      = str(os.getenv('DJANGO_DEBUG')).lower() in ('1', 'true', 'yes', 'on')

DOMAIN_NAME   = os.getenv('DJANGO_DOMAIN_NAME', 'localhost')
ALLOWED_HOSTS = [stripped_host for host in os.getenv('DJANGO_ALLOWED_HOSTS', 'localhost').split(',') if (stripped_host:=host.strip())]

PARENT_NAME = Path(__file__).resolve().parent.name

DJANGO_CELERY_BROKER_URL = os.getenv('DJANGO_CELERY_BROKER_URL', 'redis://localhost:6379/0')

INFERENCE_HOST = os.getenv('INFERENCE_HOST', 'localhost')
INFERENCE_PORT = os.getenv('INFERENCE_PORT', '8001')
INFERENCE_URL = f"http://{INFERENCE_HOST}:{INFERENCE_PORT}"
INFERENCE_INGEST_TIMEOUT = float(os.getenv('INFERENCE_INGEST_TIMEOUT', '600'))

STATIC_URL  = os.getenv('DJANGO_STATIC_URL',  '/static/')
MEDIA_URL   = os.getenv('DJANGO_MEDIA_URL',  '/media/')
STATIC_ROOT = os.getenv('DJANGO_STATIC_ROOT', BASE_DIR / 'static')
MEDIA_ROOT  = os.getenv('DJANGO_MEDIA_ROOT',  BASE_DIR / 'media')

DB_ENGINE = os.getenv('DJANGO_DB_ENGINE', 'django.db.backends.sqlite3')
DB_NAME = os.getenv('POSTGRES_DB', BASE_DIR / 'db.sqlite3')
DB_USER = os.getenv('POSTGRES_USER')
DB_PASSWORD = os.getenv('POSTGRES_PASSWORD')
DB_HOST = os.getenv('POSTGRES_HOST')
DB_PORT = os.getenv('POSTGRES_PORT', 5432)

if any(arg.startswith('test') for arg in sys.argv):
    DB_ENGINE = 'django.db.backends.sqlite3'
    DB_NAME = ':memory:'
    DB_USER = None
    DB_PASSWORD = None
    DB_HOST = None
    DB_PORT = None

OVERRIDE_APPS = [
                    
               
    'daphne',
]
DJANGO_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
THIRD_PARTY_APPS = [
    'rest_framework',
    'django_filters',
    'channels',
    'django_celery_results',
    'corsheaders',
]
LOCAL_APPS = [
    'apps.accounts',
    'apps.onboarding',
    'apps.knowledge',
]
INSTALLED_APPS = OVERRIDE_APPS + DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

AUTH_USER_MODEL = 'accounts.User'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
 'whitenoise.middleware.WhiteNoiseMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    '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     = f'{PARENT_NAME}.urls'
WSGI_APPLICATION = f'{PARENT_NAME}.wsgi.application'
ASGI_APPLICATION = f'{PARENT_NAME}.asgi.application'

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            'hosts': [DJANGO_CELERY_BROKER_URL],
        },
    },
}

SESSION_ENGINE = 'django.contrib.sessions.backends.db'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

DATABASES = {
 'default': {
        'ENGINE': DB_ENGINE,
        'NAME': DB_NAME,
    } if DB_ENGINE == 'django.db.backends.sqlite3' else {
  'ENGINE': DB_ENGINE,
  'NAME': DB_NAME,
  'USER': DB_USER,
  'PASSWORD': DB_PASSWORD,
  'HOST': DB_HOST,
  'PORT': DB_PORT,
  'CONN_MAX_AGE': 600,
 }
}

STORAGES = {
    "default": {
        "BACKEND": "django.core.files.storage.FileSystemStorage",
    },
    "staticfiles": {
        "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
    },
}

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-uk'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
    'DEFAULT_FILTER_BACKENDS': [
        'django_filters.rest_framework.DjangoFilterBackend',
    ],
}

CELERY_BROKER_URL = DJANGO_CELERY_BROKER_URL
CELERY_RESULT_BACKEND = 'django-db'
CELERY_CACHE_BACKEND = 'django-cache'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'UTC'
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60

X_FRAME_OPTIONS = 'SAMEORIGIN'
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = [
    f'http://{DOMAIN_NAME}',
    f'https://{DOMAIN_NAME}',
]
CSRF_TRUSTED_ORIGINS = [
    f'http://{DOMAIN_NAME}',
    f'https://{DOMAIN_NAME}',
]
CSRF_COOKIE_HTTPONLY = False
CSRF_COOKIE_SECURE = not DEBUG
CSRF_COOKIE_SAMESITE = 'Lax'
SESSION_COOKIE_SAMESITE = 'Lax'
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SECURE = not DEBUG
SESSION_COOKIE_AGE = 1209600
SESSION_SAVE_EVERY_REQUEST = True

if DEBUG:
    CORS_ALLOWED_ORIGINS.append(f'http://{DOMAIN_NAME}:5173')
    CORS_ALLOWED_ORIGINS.append(f'http://{DOMAIN_NAME}:8000')
    CSRF_TRUSTED_ORIGINS.append(f'http://{DOMAIN_NAME}:5173')
    CSRF_TRUSTED_ORIGINS.append(f'http://{DOMAIN_NAME}:8000')
