import os

from pathlib import Path
from dotenv import load_dotenv
from .git_utils import get_git_commit_hash

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

load_dotenv()

# Git commit hash
GIT_COMMIT_HASH = get_git_commit_hash()

DB_HOST = os.getenv('DB_HOST', '127.0.0.1')
DB_PORT = int(os.getenv('DB_PORT', 3306))
DB_NAME = os.getenv('DB_NAME', 'default')
DB_USER = os.getenv('DB_USER', 'default_user')
DB_PASSWORD = os.getenv('DB_PASSWORD', '')
DB_ROOT_PASSWORD = os.getenv('DB_ROOT_PASSWORD', '')
DB_PATH = os.getenv('DB_PATH', './mariadb-data')
SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-@8e9&qxpwe59^lxa2v&hee1q=%%3*kr#g74k^id328g9m4*2=l')
PASSWORD = os.getenv('PASSWORD', 'SetMeYouFuckingIdiot')
DEBUG = os.getenv('DEBUG', 'False').lower() == 'true'

# Defines a display version for templates
APP_VERSION = f"{'dev' if DEBUG else 'prod'} @ {GIT_COMMIT_HASH}"

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'common',
    'api',
    'landing',
    'library',
    'profiles',
    'announcements',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'common.middleware.GateMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'common.middleware.TimingMiddleware',
]

ROOT_URLCONF = 'common.urls'

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

WSGI_APPLICATION = 'common.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASSWORD,
        'HOST': DB_HOST,
        'PORT': DB_PORT,
    }
}


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


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Bogota'

USE_I18N = True

USE_TZ = True

ALLOWED_HOSTS = ['127.0.0.1', 'jakerasp', 'jakerasp.rainbow-herring.ts.net', 'localhost']

# Sessions settings
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
SESSION_COOKIE_AGE = 86400

# Production Server BS over here
CSRF_TRUSTED_ORIGINS = [
    'https://jakerasp.rainbow-herring.ts.net',
    'http://127.0.0.1'
    'http://jakerasp',
    'http://localhost'
]
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
USE_X_FORWARDED_HOST = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

DATA_UPLOAD_MAX_MEMORY_SIZE = 1073741824   # 1 GB
FILE_UPLOAD_MAX_MEMORY_SIZE = 1073741824

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/6.0/howto/static-files/

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

# Media (pack downloads, user assets) — served through Django behind the gate
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

# Login settings
LOGIN_URL = 'profiles:login'
LOGIN_REDIRECT_URL = '/'

# Gate settings — URL the GateMiddleware redirects HTML pages to
GATE_URL = '/'

SESSION_EXPIRE_AT_BROWSER_CLOSE = False