"""
Django settings for dehkade project.

Generated by 'django-admin startproject' using Django 4.2.19.

For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""

from pathlib import Path
import pymysql
pymysql.install_as_MySQLdb()

# 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/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-ex-3lm(5edc16*0n3=uk73t1nq784p4u%%sfqmita#soh3-aam'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ["www.technodehkade.ir" , "www.noavaranfardaa.ir" , "technodehkade.ir" , "noavaranfardaa.ir"]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home',
    'portfolio',
    'rest_framework',
    'contact',
    'ecommerce',
    'channels',
    'chat',  # اپ جدید چت
    'captcha',

]


CAPTCHA_LETTER_ROTATION = (-25, 25)
CAPTCHA_BACKGROUND_COLOR = '#ffffff'

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 = 'dehkade.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [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 = 'dehkade.wsgi.application'
ASGI_APPLICATION = 'dehkade.asgi.application'


# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': config('DB_ENGINE', default='django.db.backends.mysql'),
        'NAME': config('DB_NAME'),
        'USER': config('DB_USER'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST': config('DB_HOST', default='localhost'),
        'PORT': config('DB_PORT', default='3306'),
        'OPTIONS': {
            'init_command': config('DB_OPTIONS_INIT', default="SET sql_mode='STRICT_TRANS_TABLES'")
        }
    }
}

# Password validation
# https://docs.djangoproject.com/en/4.2/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/4.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

import os

STATIC_URL = '/static/'
STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')



import os

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'


SECURE_SSL_REDIRECT = True


# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

AUTH_USER_MODEL = 'home.CustomUser'
# --- Email Settings for Production ---
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.technodehkade.ir'
EMAIL_USE_SSL = True              # چون پورت 465 هست
EMAIL_PORT = 465
EMAIL_HOST_USER = 'support@technodehkade.ir'
EMAIL_HOST_PASSWORD = 'Adonis64@Adonis64'
DEFAULT_FROM_EMAIL = 'support@technodehkade.ir'
SERVER_EMAIL = 'support@technodehkade.ir'  # ایمیلی که خطاهای 500 ازش ارسال می‌شه
DEFAULT_FROM_EMAIL = 'دهکده نوآوران فردا <support@technodehkade.ir>'


# --- امنیت ---
SECURE_HSTS_SECONDS = 31536000        # فعال‌سازی HSTS برای 1 سال
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True

SECURE_CONTENT_TYPE_NOSNIFF = True    # جلوگیری از حدس نوع فایل
SECURE_BROWSER_XSS_FILTER = True       # فعال کردن فیلتر XSS مرورگر
SESSION_COOKIE_SECURE = True           # کوکی‌ها فقط روی HTTPS ارسال بشن
CSRF_COOKIE_SECURE = True
X_FRAME_OPTIONS = 'DENY'               # جلوگیری از iframe injection

CSRF_TRUSTED_ORIGINS = [
    "https://noavaranfardaa.ir",
    "https://www.noavaranfardaa.ir",
]


# در صورت نیاز:
CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels.layers.InMemoryChannelLayer"
    }
}


