from pathlib import Path
import os
import dj_database_url
from dotenv import load_dotenv

load_dotenv()

BASE_DIR = Path(__file__).resolve().parent.parent
BASE_URL = "https://app.polisplexity.tech"

import sys
sys.path.append(str(BASE_DIR))


# Core security settings
SECRET_KEY = os.getenv("SECRET_KEY")
DEBUG = os.getenv("DEBUG", "False") == "True"
import os

_raw = os.getenv("ALLOWED_HOSTS", "")
ALLOWED_HOSTS = [h.strip() for h in _raw.split(",") if h.strip()]  # from .env if present

# Hotfix: always allow local calls from inside container & host mapping
for h in ("127.0.0.1", "localhost"):
    if h not in ALLOWED_HOSTS:
        ALLOWED_HOSTS.append(h)

# Application definition
INSTALLED_APPS = [
    # Django built-in apps
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "django.contrib.sites",  # Required for allauth

    # Allauth
    "allauth",
    "allauth.account",
    "allauth.socialaccount",
    "allauth.socialaccount.providers.github",  # GitHub login only

    # Your custom apps
    "core",
    "pxy_de",
    "pxy_cr",
    "pxy_whatsapp",
    "pxy_city_digital_twins",
    "pxy_bots",
    "pxy_openai",
    "pxy_meta_pages",
    "pxy_langchain",
    "pxy_neo4j",
    "pxy_dashboard",
    "pxy_dashboard.custom",
    "pxy_dashboard.apps",
    "pxy_dashboard.components",
    "pxy_dashboard.layouts",
    "pxy_building_digital_twins",
    "pxy_messenger",
    'pxy_contracts',
    'pxy_sami',
    'pxy_routing',
    'pxy_sites',
    "pxy_simulations",
    "pxy_stewards",

    "rest_framework",
    "pxy_api",
    'pxy_agents_coral',


    # Third-party apps
    "crispy_forms",
    "crispy_bootstrap5",

]

CRISPY_ALLOWED_TEMPLATE_PACKS = "bootstrap5"
CRISPY_TEMPLATE_PACK = "bootstrap5"

SITE_ID = int(os.getenv("SITE_ID", 1))

AUTHENTICATION_BACKENDS = [
    "django.contrib.auth.backends.ModelBackend",  # default
    "allauth.account.auth_backends.AuthenticationBackend",  # allauth support
]

LOGIN_REDIRECT_URL = "/"
ACCOUNT_LOGOUT_REDIRECT_URL = "/accounts/login/"


MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "allauth.account.middleware.AccountMiddleware",  
    "pxy_dashboard.middleware.LoginRequiredMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "polisplexity.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [
            os.path.join(BASE_DIR, "templates"),
            os.path.join(BASE_DIR, "pxy_dashboard", "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",
                "pxy_dashboard.context_processors.sidebar_context",
            ],
        },
    },
]



WSGI_APPLICATION = "polisplexity.wsgi.application"

# Database
DATABASES = {
    "default": dj_database_url.config(default=os.getenv("DATABASE_URL"))
}

# Password validation
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
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True

# Static & Media Files
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "polisplexity/pxy_dashboard/static"),  # Jidox assets
]

MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
SERVE_MEDIA = os.getenv("SERVE_MEDIA", "").lower() in ("1", "true", "yes")


# Default primary key field type
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# External services
PAGE_ACCESS_TOKEN = os.getenv("PAGE_ACCESS_TOKEN")
VERIFY_TOKEN = os.getenv("VERIFY_TOKEN")

# Async-safe for Neo4j or Celery
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
# ...pero silenciamos la comprobación que falla en producción:
SILENCED_SYSTEM_CHECKS = ["async.E001"]

# Neo4j
NEO4J_URI = os.getenv("NEO4J_URI")
NEO4J_USERNAME = os.getenv("NEO4J_USERNAME")
NEO4J_PASSWORD = os.getenv("NEO4J_PASSWORD")

# OpenAI
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

# Cesium ion
CESIUM_ION_TOKEN = os.getenv("CESIUM_ION_TOKEN", "")

# CSRF protection for production
CSRF_TRUSTED_ORIGINS = [
    "https://app.polisplexity.tech",
]

# Support for secure reverse proxy (e.g., Nginx or Hostinger HTTPS proxy)
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")


EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp.hostinger.com"
EMAIL_PORT = 465
EMAIL_USE_SSL = True
EMAIL_HOST_USER = "noreply@polisplexity.tech"  # Cambia esto por tu correo real
EMAIL_HOST_PASSWORD = os.getenv("EMAIL_HOST_PASSWORD")  # Mejor usar .env
DEFAULT_FROM_EMAIL = "Polisplexity <noreply@polisplexity.tech>"

MESSENGER_VERIFY_TOKEN = os.getenv("MESSENGER_VERIFY_TOKEN", "dev-change-me")

FACEBOOK_APP_SECRET = os.getenv("FACEBOOK_APP_SECRET", "")  # set this in .env for prod

REST_FRAMEWORK = {
    # Deshabilitamos auth por ahora para evitar CSRF en curl
    "DEFAULT_AUTHENTICATION_CLASSES": [],

    # Throttling global + por-scope
    "DEFAULT_THROTTLE_CLASSES": [
        "rest_framework.throttling.AnonRateThrottle",
        "rest_framework.throttling.UserRateThrottle",
        "rest_framework.throttling.ScopedRateThrottle",
    ],
    "DEFAULT_THROTTLE_RATES": {
        "anon": "100/hour",
        "user": "1000/hour",
        "sami_run": "30/minute",
        "sites_search": "15/minute",
        "routing_isochrone": "60/minute",
        "routing_health": "120/minute",
        "sami_health": "120/minute",
        "sites_health": "120/minute",
    },

    # Manejo de errores uniforme
    "EXCEPTION_HANDLER": "pxy_api.exceptions.envelope_exception_handler",
}

AGENTS_INTERNAL_BASE = os.getenv("AGENTS_INTERNAL_BASE", "")

LOGGING = {
    # ... keep your existing handlers/formatters ...
    "version": 1,
    "disable_existing_loggers": False,
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
        },
    },
    "loggers": {
        "pxy_bots": {                 # package logger
            "handlers": ["console"],
            "level": "INFO",          # or "DEBUG"
            "propagate": False,
        },
        # make sure __name__ in router resolves here; if your module path is different, use that
        "pxy_bots.router": {
            "handlers": ["console"],
            "level": "INFO",
            "propagate": False,
        },
    },
}

# honor reverse proxy + allow “Host” from proxy
USE_X_FORWARDED_HOST = True
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

# optional hard override (recommended in prod)
PUBLIC_BASE_URL = os.getenv("PUBLIC_BASE_URL", "").rstrip("/")
