"""
Django settings for xygaming_core project.

Generated by 'django-admin startproject' using Django 3.0.8.

For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os
import sys

if sys.version_info >= (3, 14):
    try:
        from django.template.context import Context
        def _context_copy(self):
            duplicate = Context()
            duplicate.dicts = self.dicts[:]
            return duplicate
        Context.__copy__ = _context_copy
    except Exception:
        pass

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'z+wh@krj_*v&fok-=fdwr=5(i$4zsd+&q(df0)7$g5e(d02!9&'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DJANGO_DEBUG', 'False').lower() in ('true', '1', 't')
TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = ['xygame.co', 'www.xygame.co', 'localhost', '127.0.0.1', '*']


# Application definition

INSTALLED_APPS = [
    'xygaming_app',
    'cart',
    'adminapp',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'crispy_forms',
    'blog',
    'currencies',
]
SITE_ID = 1
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
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',
]

ROOT_URLCONF = 'xygaming_core.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'templates', 'accounts')],
        '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',
                'cart.context_processor.cart_total_amount',
                'currencies.context_processors.currencies',

            ],
            'libraries':{
                'index': 'xygaming_app.template_tags.custom_tags',
                'poll_extras': 'adminapp.templatetags.poll_extras',

            }
        },
    },
]
CART_SESSION_ID = 'cart'
AUTHENTICATION_BACKENDS = [

    'django.contrib.auth.backends.ModelBackend',

    'allauth.account.auth_backends.AuthenticationBackend',
]

WSGI_APPLICATION = 'xygaming_core.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

# DATABASES = {
#   'default': {
#       'ENGINE': 'django.db.backends.sqlite3',
#       'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#   }
# }


USE_SQLITE = os.environ.get('USE_SQLITE', 'False').lower() in ('true', '1', 't')
FORCE_MYSQL = os.environ.get('PRODUCTION', 'True').lower() in ('true', '1', 't') and not USE_SQLITE

# Detect available MySQL engine
mysql_engine = 'django.db.backends.mysql'
try:
    import mysql.connector
    mysql_engine = 'mysql.connector.django'
except ImportError:
    pass

mysql_config = {
    'ENGINE': mysql_engine,
    'NAME': 'xygameco_xygaming',
    'USER': 'xygameco_xyadmin',
    'PASSWORD': '=RJoxh@aouHx',
    'HOST': 'localhost',
    'PORT': '3306',
    'CONN_MAX_AGE': 60,
    'OPTIONS': {
        'charset': 'utf8mb4',
    }
}

sqlite_config = {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    'TEST': {
        'SERIALIZE': False,
    }
}

if FORCE_MYSQL:
    DATABASES = {'default': mysql_config}
else:
    # Auto-detect local MySQL server
    try:
        if mysql_engine == 'mysql.connector.django':
            import mysql.connector
            conn = mysql.connector.connect(
                user='xygameco_xyadmin',
                password='=RJoxh@aouHx',
                host='localhost',
                port=3306,
                connection_timeout=2
            )
            conn.close()
        else:
            import pymysql
            conn = pymysql.connect(
                user='xygameco_xyadmin',
                password='=RJoxh@aouHx',
                host='localhost',
                port=3306,
                connect_timeout=2
            )
            conn.close()
        DATABASES = {'default': mysql_config}
    except Exception:
        DATABASES = {'default': sqlite_config}



# Password validation
# https://docs.djangoproject.com/en/3.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',
    },
]
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'xygame.co'
EMAIL_PORT = 587
EMAIL_USE_SSL = False
EMAIL_HOST_USER = 'accounts@xygame.co'
DEFAULT_FROM_EMAIL = 'accounts@xygame.co'
EMAIL_HOST_PASSWORD = 'u6gCTiZ7LSqa'
#######################
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_VERIFICATION = "mandatory"

# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kathmandu'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

ACCOUNT_FORMS = {'signup': 'xygaming_app.forms.MyCustomSignupForm'}

CRISPY_TEMPLATE_PACK = 'bootstrap4'

CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = 'Strict'
SESSION_COOKIE_SAMESITE = 'Strict'

DEFAULT_CURRENCY = "NPR"
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

