import os
import secrets
import warnings

# ─── Database ──────────────────────────────────────────────────────────────
MASTER_DB_URL = os.environ.get("MASTER_DB_URL")
TENANT_DB_URL_TEMPLATE = os.environ.get("TENANT_DB_URL_TEMPLATE")

if not MASTER_DB_URL:
    raise ValueError(
        "MASTER_DB_URL environment variable is required. "
        "Example: postgresql://user:pass@localhost/nexus_autoparts"
    )
if not TENANT_DB_URL_TEMPLATE:
    raise ValueError(
        "TENANT_DB_URL_TEMPLATE environment variable is required. "
        "Example: postgresql://user:pass@localhost/{db_name}"
    )

# ─── JWT Authentication ────────────────────────────────────────────────────
JWT_SECRET = os.environ.get("POS_JWT_SECRET")
if not JWT_SECRET:
    raise ValueError(
        "POS_JWT_SECRET environment variable is required. "
        "Generate one with: python3 -c 'import secrets; print(secrets.token_hex(32))'"
    )

JWT_ACCESS_EXPIRES = 28800    # 8 hours (full shift)
JWT_REFRESH_EXPIRES = 2592000 # 30 days

# ─── PIN Security ──────────────────────────────────────────────────────────
PIN_MAX_ATTEMPTS_PER_MINUTE = 5
PIN_LOCKOUT_THRESHOLD = 10
PIN_LOCKOUT_MINUTES = 15

TENANT_TEMPLATE_DB = "tenant_template"

# ─── AI / OpenRouter ───────────────────────────────────────────────────────
OPENROUTER_API_KEY = os.environ.get("OPENROUTER_API_KEY")
if not OPENROUTER_API_KEY:
    warnings.warn(
        "OPENROUTER_API_KEY not set. AI chatbot features will be disabled.",
        RuntimeWarning
    )

# ─── Hermes Agent ──────────────────────────────────────────────────────────
HERMES_API_URL = os.environ.get("HERMES_API_URL", "http://192.168.10.71:8642/v1")
HERMES_API_KEY = os.environ.get("HERMES_API_KEY")
if not HERMES_API_KEY:
    warnings.warn(
        "HERMES_API_KEY not set. Hermes Agent integration will fall back to OpenRouter.",
        RuntimeWarning
    )

# ─── SMTP ──────────────────────────────────────────────────────────────────
SMTP_HOST = os.environ.get('SMTP_HOST', 'smtp.gmail.com')
SMTP_PORT = int(os.environ.get('SMTP_PORT', '587'))
SMTP_USER = os.environ.get('SMTP_USER', '')
SMTP_PASS = os.environ.get('SMTP_PASS', '')
SMTP_FROM = os.environ.get('SMTP_FROM', 'noreply@nexusautoparts.com.mx')

# ─── WhatsApp Bridge ───────────────────────────────────────────────────────
WHATSAPP_BRIDGE_URL = os.environ.get('WHATSAPP_BRIDGE_URL', 'http://localhost:21465')
WHATSAPP_BRIDGE_KEY = os.environ.get('WHATSAPP_BRIDGE_KEY')
if not WHATSAPP_BRIDGE_KEY:
    warnings.warn(
        "WHATSAPP_BRIDGE_KEY not set. WhatsApp integration will be disabled.",
        RuntimeWarning
    )

# ─── Multi-currency ────────────────────────────────────────────────────────
DEFAULT_CURRENCY = os.environ.get('DEFAULT_CURRENCY', 'MXN')
EXCHANGE_RATE_USD_MXN = float(os.environ.get('EXCHANGE_RATE_USD_MXN', '17.5'))

# ─── Redis Cache ───────────────────────────────────────────────────────────
REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379/0')
REDIS_ENABLED = os.environ.get('REDIS_ENABLED', 'true').lower() == 'true'
REDIS_STOCK_TTL = int(os.environ.get('REDIS_STOCK_TTL', '300'))

# ─── Meilisearch ───────────────────────────────────────────────────────────
MEILI_URL = os.environ.get('MEILI_URL', 'http://localhost:7700')
MEILI_API_KEY = os.environ.get('MEILI_API_KEY', '')
MEILI_ENABLED = os.environ.get('MEILI_ENABLED', 'true').lower() == 'true'

# ─── Catalog OEM Access ────────────────────────────────────────────────────
CATALOG_OEM_ENABLED = os.environ.get('CATALOG_OEM_ENABLED', 'false').lower() == 'true'

# ─── QWEN AI Fitment (private cloud server) ────────────────────────────────
QWEN_API_URL = os.environ.get('QWEN_API_URL', '')
QWEN_API_KEY = os.environ.get('QWEN_API_KEY', '')
QWEN_MODEL = os.environ.get('QWEN_MODEL', 'qwen3.6')


# ─── Internal Cron / Job Security ──────────────────────────────────────────
INTERNAL_API_KEY = os.environ.get('INTERNAL_API_KEY', '')
