"""
Production settings for InterServer cPanel deployment.
Import this instead of settings.py on the server.
"""
from .settings import *
import os
from dotenv import load_dotenv

# Load production .env
load_dotenv(os.path.join(BASE_DIR, '.env.production'))

DEBUG = False

# Every name this CRM answers to. Moving it to a new domain is then a line in
# .env rather than an edit here, and both names can serve at once while DNS and
# bookmarks catch up - a hard cut-over is what turns a domain change into an
# outage. Django refuses any host not on this list, so a missing one shows up
# as "Invalid HTTP_HOST header", not as a subtle fault.
_DOMAIN = os.getenv('DOMAIN', 'altriacallcenter.com')
ALLOWED_HOSTS = [
    'crm.altriacallcenter.com',
    'crm.altriaph.com',
    _DOMAIN,
    f"www.{_DOMAIN}",
    'localhost',
    '127.0.0.1',
] + [host.strip() for host in os.getenv('EXTRA_ALLOWED_HOSTS', '').split(',') if host.strip()]

# Production MySQL Database
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.getenv('DB_NAME'),
        'USER': os.getenv('DB_USER'),
        'PASSWORD': os.getenv('DB_PASSWORD'),
        'HOST': os.getenv('DB_HOST', 'localhost'),
        'PORT': os.getenv('DB_PORT', '3306'),
        'OPTIONS': {
            'charset': 'utf8mb4',
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    },
    'email_db': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'altriaca_email',
        'USER': 'altriaca_user',
        'PASSWORD': 'Altria123!@#',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'charset': 'utf8mb4',
        },
    },
}

DATABASE_ROUTERS = ['email_reports.routers.EmailReportsRouter']

# Static files
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# CORS — allow your domain
CORS_ALLOWED_ORIGINS = [
    f"https://{_DOMAIN}",
    f"https://www.{_DOMAIN}",
    'https://crm.altriacallcenter.com',
    'https://crm.altriaph.com',
] + [f"https://{host.strip()}" for host in os.getenv('EXTRA_ALLOWED_HOSTS', '').split(',') if host.strip()]

# Django checks these on any form post that arrives over HTTPS. Without the new
# name here, signing into /admin on it fails with a CSRF error that says nothing
# about the domain being the cause.
CSRF_TRUSTED_ORIGINS = [
    f"https://{host}" for host in ALLOWED_HOSTS if host not in ('localhost', '127.0.0.1')
]

# Security (HTTPS required in production)
SECURE_BROWSER_XSS_FILTER = True
# Still DENY for everyone except the sites named in FRAME_ANCESTORS, which
# core.middleware.FrameAncestorsMiddleware permits through a CSP instead.
X_FRAME_OPTIONS = 'DENY'
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

# Email (use SMTP in production)
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
# Without this Django's SMTP backend blocks forever when the host is
# unreachable. Shared hosting filters outbound SMTP, so an unconfigured or
# blocked host used to hang the whole request until the gateway timed out --
# which is what made "Invite to Portal" fail with no usable error. Any mail
# failure must be fast so the caller can fall back.
EMAIL_TIMEOUT = 10

# Disable channels/ASGI on shared hosting — LiteSpeed uses WSGI only
INSTALLED_APPS = [app for app in INSTALLED_APPS if app != 'channels'] + ['email_reports']
ASGI_APPLICATION = None
