"""
Django settings for idealogy_api (SANDBOX).
Imports everything from settings.py, then overrides DB + CORS + DEBUG.
"""
# Install pymysql as MySQLdb BEFORE any Django imports
import pymysql
pymysql.install_as_MySQLdb()

from pathlib import Path

from .settings import *  # noqa

SECRETS_FILE = Path.home() / '.secrets_idealogy'

# ---------- Re-load secrets to override DB ----------
_secrets = {}
if SECRETS_FILE.exists():
    with open(SECRETS_FILE, encoding="utf-8") as f:
        for line in f:
            line = line.strip()
            if not line or line.startswith('#') or '=' not in line:
                continue
            key, _, value = line.partition('=')
            _secrets[key.strip()] = value.strip()

# ---------- Sandbox overrides ----------
DEBUG = True

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': _secrets.get('DB_SANDBOX_NAME'),
        'USER': _secrets.get('DB_SANDBOX_USER'),
        'PASSWORD': _secrets.get('DB_SANDBOX_PASSWORD'),
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
            'charset': 'utf8mb4',
        },
    }
}

CORS_ALLOWED_ORIGINS = [
    'https://sandbox-idealogy.orkoworo.com',
]

CSRF_TRUSTED_ORIGINS = [
    'https://sandbox-idealogy.orkoworo.com',
    'https://api-sandbox-idealogy.orkoworo.com',
]

ALLOWED_HOSTS = [
    'sandbox-idealogy.orkoworo.com',
    'api-sandbox-idealogy.orkoworo.com',
    'localhost',
    '127.0.0.1',
]

SESSION_SAVE_EVERY_REQUEST = True
