"""
Django settings for coi project.

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

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

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

from pathlib import Path
import os
from pprint import pprint
# 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/5.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-#td578+^qcir9s!0pb7lxh!4tefib7t&x7^p4+=5$vpvujjumn"

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

DJANGO_ENV = os.getenv("DJANGO_ENV", "dev")  # Default to 'dev' if not set
pprint(f"ENV = {DJANGO_ENV}")
ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    'tailwind',
    'crispy_forms',
    'crispy_tailwind',
    'django_extensions',
    'theme.apps.ThemeConfig',
    'django_browser_reload',
    "django_filters",
    "core.apps.CoreConfig",
    "report.apps.ReportConfig",
    "sysadmin.apps.SysadminConfig",
    "dashboard.apps.DashboardConfig",
    "legacy.apps.LegacyConfig",
]

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",
    "django_browser_reload.middleware.BrowserReloadMiddleware",
]

ROOT_URLCONF = "coi.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        'DIRS': [os.path.join(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 = "coi.wsgi.application"


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
DJANGO_ENV = os.getenv("DJANGO_ENV", "dev")  # Default to 'dev' if not set
if DJANGO_ENV == "dev":
    DB_NAME = os.environ.get('DB_NAME', 'coi_db')
    DB_USER =  os.environ.get('DB_USER', 'sa')
    DB_PASSWORD = os.environ.get('DB_PASSWORD', '')
    DB_HOST =  os.environ.get('DB_HOST', 'db')
    DB_PORT = os.environ.get('DB_PORT', '1433')
    DB_DRIVER = 'ODBC Driver 18 for SQL Server'
    DB_ENGINE = 'mssql'

    DB2_USER =  os.environ.get('DB_USER', 'sa')
    DB2_PASSWORD = os.environ.get('DB_PASSWORD', '')
    DB2_HOST =  os.environ.get('DB_HOST', 'db')
    DB2_PORT = os.environ.get('DB_PORT', '1433')
    DB2_DRIVER = 'ODBC Driver 18 for SQL Server'
    DB2_ENGINE = 'mssql'
else:
    DB_NAME = "coi_db"
    DB_USER =  "user1"
    DB_PASSWORD = "1234"
    DB_HOST =  "localhost\SQLEXPRESS"
    DB_PORT = '1433'
    DB_DRIVER = 'ODBC Driver 17 for SQL Server'
    DB_ENGINE = 'mssql'

    DB2_USER =  "user1"
    DB2_PASSWORD = "1234"
    DB2_HOST =  "192.168.0.253"
    DB2_PORT = '1433'
    DB2_DRIVER = 'ODBC Driver 17 for SQL Server'
    DB2_ENGINE = 'mssql'

DATABASES = {
    'default': {
        'ENGINE': DB_ENGINE,
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASSWORD,
        'HOST': DB_HOST,
        #'PORT': DB_PORT,
        'OPTIONS': {
            'driver': DB_DRIVER,
            'extra_params':  'Encrypt=no',
        },
    },
    'OB2011DB': {
        'ENGINE': DB2_ENGINE,
        'NAME': 'OB2011DB',
        'USER': DB2_USER,
        'PASSWORD': DB2_PASSWORD,
        'HOST': DB2_HOST,
        #'PORT': DB2_PORT,
        'OPTIONS': {
            'driver': DB2_DRIVER,
            'extra_params':  'Encrypt=no',
        },
    },
    'MGT_Trans': {
        'ENGINE': DB2_ENGINE,
        'NAME': 'MGT_Trans',
        'USER': DB2_USER,
        'PASSWORD': DB2_PASSWORD,
        'HOST': DB2_HOST,
        #'PORT': DB2_PORT,
        'OPTIONS': {
            'driver': DB2_DRIVER,
            'extra_params':  'Encrypt=no',
        },
    },
}


# Password validation
# https://docs.djangoproject.com/en/5.1/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/5.1/topics/i18n/

LANGUAGE_CODE = "en-us"

#TIME_ZONE = "Asia/Bangkok"

USE_I18N = True

#USE_TZ = True


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

STATIC_URL = "static/"

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

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# settings.py

# Database Connection Strings
DATABASE_CONNECTIONS = {
    "prod": {
        "DRIVER": "{SQL Server}",
        "SERVER": "192.168.0.253",
        "DATABASE": "OB2011DB",
        "UID": "user1",
        "PWD": "1234",
    },
    "dev": {
        "DRIVER": "{SQL Server}",
        "SERVER": "localhost",
        "DATABASE": "OB2011DB",
        "UID": "admin",
        "PWD": "1234",
    },
}

# Media files (uploaded files and generated files)
MEDIA_URL = '/media/'  # URL that handles the media served from MEDIA_ROOT
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')  # Absolute path to the directory where media is stored

STATICFILES_DIRS = [
    BASE_DIR / "static",
    BASE_DIR / "node_modules"
]
TAILWIND_APP_NAME = 'theme'
INTERNAL_IPS = [
    "127.0.0.1",
]
CRISPY_ALLOWED_TEMPLATE_PACKS = "tailwind"
CRISPY_TEMPLATE_PACK = "tailwind"

LOGIN_URL = '/sysadmin/login/'  # Replace with the path to your login view
LOGIN_REDIRECT_URL = '/sysadmin/profile/'  # Optional

DATABASE_ROUTERS = ["coi.routers.DatabaseRouter"]

