"""
Django settings for rpplaza project.

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

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

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

from pathlib import Path
import sys
import os
from decouple import config

# Django secret key
SECRET_KEY = config('DJANGO_SECRET_KEY')

# Debug mode
DEBUG = config('DJANGO_DEBUG', default=False, cast=bool)

# 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.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
# SECRET_KEY = 'django-insecure-b$)(673onhe8*8&vg^#tqo4al(vpsa-@jfifnho_oab(or(epc'

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

ALLOWED_HOSTS = [
    "rpplaza.com",
    "www.rpplaza.com",
]
#SITE_URL = "http://127.0.0.1:8000" #local
SITE_URL = "http://www.rpplaza.com" #production
#SITE_URL = "http://www.dev.rpplaza.com"  #dev


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'admin_group_restrictions',
    "django.contrib.humanize",
    'marketplace',
    "accounts",
    "django.contrib.sites",
    "allauth",
    "allauth.account",
    "allauth.socialaccount",
    # Provider (start with Google)
    "allauth.socialaccount.providers.google",
    "allauth.socialaccount.providers.facebook",      
    "stores",
    "locations",    
    "campaigns"
]


MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', 
    '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',
    "allauth.account.middleware.AccountMiddleware",      
    "marketplace.middleware.PageViewMiddleware",
    
]

ROOT_URLCONF = 'config.urls'
LOGIN_URL = '/accounts/login/'
LOGOUT_REDIRECT_URL = '/'
# LOGIN_REDIRECT_URL = '/redirect-after-login/'
LOGIN_REDIRECT_URL = "/accounts/redirect/"

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',
                'marketplace.context_processors.user_menu',
                # 'core.context_processors.menu_items',
                "marketplace.context_processors.profile_incomplete",                
                "marketplace.context_processors.user_store",
                "django.template.context_processors.media",
            ],
        },
    },
]

WSGI_APPLICATION = 'config.wsgi.application'


# Database
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': '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': {
            'charset': 'utf8mb4',
        },
    }
}


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

LANGUAGE_CODE = 'es'

TIME_ZONE = 'America/Santo_Domingo'

USE_I18N = True

USE_TZ = True


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

STATIC_URL = "/static/"
STATICFILES_DIRS = [
    BASE_DIR / "static",
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

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

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

APPEND_SLASH = True

# ------------------------------
# DASHBOARD REDIRECTION MAP
# ------------------------------
USER_DASHBOARD_MAP = {
    # Administración groups
    "Adm-Administrativo": "administracion:dashboard"    
}


# settings.py (append or edit)
DASHBOARD_ACCESS = {
    # key -> allowed group names
    "administracion": [
        "Adm-Administrativo",
    ],
   
    # add more app keys as needed for other divisiones and departments
}

HANDLER403 = "core.views.custom_permission_denied_view"
# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# DEFAULT_FROM_EMAIL = 'no-reply@rhapp.com'

#SITE_ID = 1 #local
SITE_ID = 6 #production
AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend",
]

ACCOUNT_SIGNUP_FIELDS = ["email*", "password1*", "password2*",]
ACCOUNT_EMAIL_VERIFICATION = "mandatory"
ACCOUNT_LOGIN_METHODS = {"email"}
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = True
ACCOUNT_EMAIL_CONFIRMATION_EXPIRE_DAYS = 2
ACCOUNT_LOGIN_METHODS = {"email"}
ACCOUNT_SESSION_REMEMBER = True
LOGIN_REDIRECT_URL = "/"
ACCOUNT_LOGOUT_REDIRECT_URL = "/"
# ACCOUNT_SIGNUP_FIELDS = ["email*", "password1*", "password2*"]
# ACCOUNT_EMAIL_VERIFICATION = "none"   # temporarily for testing
# SOCIALACCOUNT_URL_PREFIX = ""  # no extra prefix
SOCIALACCOUNT_ADAPTER = "accounts.adapters.DebugSocialAccountAdapter"
AUTH_USER_MODEL = "accounts.User"
# ACCOUNT_USER_MODEL_USERNAME_FIELD = "username"
# ACCOUNT_SIGNUP_FIELDS = [
#     'email*',
#     'password1*',
#     'password2*',
# ]

ACCOUNT_SIGNUP_FIELDS = ['email*', 'password1*', 'password2*']
ACCOUNT_UNIQUE_EMAIL = True
SOCIALACCOUNT_AUTO_SIGNUP = True
SOCIALACCOUNT_QUERY_EMAIL = True
# ACCOUNT_USERNAME_REQUIRED = False


#SMTP email configuration for Django

#EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'mail.rpplaza.com'           # your SMTP host
EMAIL_PORT = 465                               # SSL port
EMAIL_USE_TLS = False                          # Disable TLS since using SSL
EMAIL_USE_SSL = True                           # Enable SSL
EMAIL_HOST_USER = 'administracion@rpplaza.com'      # your SMTP username
EMAIL_HOST_PASSWORD = config('SMTP_PASSWORD')         # your SMTP password
DEFAULT_FROM_EMAIL = 'Administración <administracion@rpplaza.com>'
CONTACT_EMAIL = "josemiguelsf@gmail.com"
# override for testing
# EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"


SILENCED_SYSTEM_CHECKS = ["models.W036"]
MAX_FREE_POSTS = 15

SOCIALACCOUNT_PROVIDERS = {

    'google': {
        'SCOPE': [
            'profile',
            'email',
        ],
        'AUTH_PARAMS': {
            'access_type': 'online',
        },
    },

'facebook': {
    'METHOD': 'oauth2',
    'SCOPE': ['email'],
    'FIELDS': [
        'id',
        'email',
        'name',
        'first_name',
        'last_name',
    ],
    'AUTH_PARAMS': {
        'auth_type': 'rerequest'
    },
}

}