"""
Django settings for visitor_management project.

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

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

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

from pathlib import Path
import os
from django.db import connection
# 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.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-p=j6kg6h*i2edfa6r(dnhvoa%@yf+ecm9j!p8p1@1y2*+-s-(v'

DOMAIN_URL ='https://vadilal.easyvisit.in/'

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

ALLOWED_HOSTS = ['vadilal.easyvisit.in']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'visitors_app',
    'gate_keeper_app',
    'employee_app',
    'admin_app',
    'api',
    'corsheaders',
    'rest_framework'
]

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',
    'corsheaders.middleware.CorsMiddleware',
    'visitor_management.middleware.HandleAllErrorsMiddleware',
]

ROOT_URLCONF = 'visitor_management.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',
                'visitors_app.context_processors.my_constants',
            ],
        },
    },
]

WSGI_APPLICATION = 'visitor_management.wsgi.application'


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

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': BASE_DIR / 'db.sqlite3',
#     }
# }

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'easyvisit_vadilal',
        'USER': 'easyvisit_vadilal',
        'PASSWORD': 'T-m%dCY}cML.',
        'HOST':'localhost',
        'PORT':'3306',
    }
}
# Password validation
# https://docs.djangoproject.com/en/5.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/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_TZ = True


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

STATIC_URL = 'static/'
# MEDIA_URL = ''
STATICFILES_DIRS = [BASE_DIR / "static"]

MEDIA_URL = '/'

MEDIA_ROOT = BASE_DIR
# MEDIA_ROOT = BASE_DIR
# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


ADMIN_STATIC_PATH = f'{DOMAIN_URL}/visitor_management/static/'

DOMAIN_NAME = 'VMS'

# DOMAIN_ICON = '/image/VMS.ico'/

# GET_PASS_IMAGE= '/image/logo.png'

# GET_PASS_IMAGE= '/image/Hexagon-infosoft.jpg'

BACKGROUND_IMAGE = '/image/bg-photo.jpg'

DATABASE_NAME = DATABASES['default']['NAME']

CORS_ORIGIN_ALLOW_ALL = True

CORS_ALLOWED_ORIGINS =['https://vadilal.easyvisit.in']

REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': ('rest_framework.renderers.JSONRenderer',),

}

# DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# EMAIL_HOST = 'smtp.gmail.com'
# EMAIL_PORT =587
# EMAIL_HOST_USER='app.hexagon@gmail.com'
# EMAIL_HOST_PASSWORD='plrr qnyl gsdb gcjl'
# EMAIL_USE_TLS=True


# DATABASE_TABLE = 'website_setting'
# sql_query = f"SELECT * FROM {DATABASE_TABLE}"
# with connection.cursor() as cursor:
#     cursor.execute(sql_query)
#     database_all_data = cursor.fetchall()
#     columns = [col[0] for col in cursor.description]
#     all_coupon_dtl = [dict(zip(columns, row)) for row in database_all_data]
# passwords = [user['smtp_password'] for user in all_coupon_dtl if 'smtp_password' in user]
# host = [item['smtp_host'] for item in all_coupon_dtl if 'smtp_host' in item]
# smtp_user = [item['smtp_user'] for item in all_coupon_dtl if 'smtp_user' in item]
# from_names = [item['from_name'] for item in all_coupon_dtl if 'from_name' in item]
# from_emails = [item['from_email'] for item in all_coupon_dtl if 'from_email' in item]
# if host:
#     smtp_host = host[0]
# else:
#     smtp_host = None

# if smtp_user:
#     smtp_user_name = smtp_user[0]
# else:
#     smtp_user_name = None

# if passwords:
#     smtp_password = passwords[0]
# else:
#     smtp_password = None
    
# if from_names:
#     from_name = from_names[0]
# else:
#     from_name = None

# if from_emails:
    
#     from_email = from_emails[0]
# else:
#     from_email = None

# DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# EMAIL_HOST = smtp_host
# EMAIL_PORT =465
# EMAIL_HOST_USER = smtp_user_name
# EMAIL_HOST_PASSWORD= smtp_password
# EMAIL_USE_SSL = True  # If using port 587, set this to False and use EMAIL_USE_TLS = True
# DEFAULT_FROM_EMAIL = f'{from_names} <{from_email}>'
# From_Email = f'{from_names} <{from_email}>'

EMAIL_BACKEND = 'visitor_management.dynamic_smtp_backend.DynamicEmailBackend'

# fallback default if DB has no values yet
DEFAULT_FROM_EMAIL = 'Admin <admin@example.com>'

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'ERROR',
            'class': 'logging.FileHandler',
            'filename': 'django_errors.log',  # Log file location
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',  # Outputs to the console
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file', 'console'],
            'level': 'ERROR',
            'propagate': True,
        },
        'visitor_management': {  # Replace 'yourapp' with the name of your app
            'handlers': ['file', 'console'],
            'level': 'ERROR',
            'propagate': True,
        },
    },
}


WHATSAPP_SEND_MESSAGE_COUNT_LIMIT = 1000
WHATSAPP_LIMIT_EXCEEDED_WARNING = 980

SESSION_COOKIE_AGE = 7200   # 2 hrs
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_SAVE_EVERY_REQUEST = False
