import os

# ==================== LICENSE ====================
# License settings — loaded from settings.json (app config), with env var override.
# Priority:
#   1. Environment variable           — dev/CI overrides
#   2. settings.json [license] section — user-configurable via Settings UI
#   3. ~/.rivet/public_key.json        — auto-updated from server on each online validation
def _get_license_setting(key: str, fallback: str = "") -> str:
    if env_val := os.getenv(key):
        return env_val
    try:
        from app.api.settings import load_settings
        settings = load_settings()
        return getattr(settings.license, key.lower(), fallback) or fallback
    except Exception:
        return fallback

OFFLINE_PUBLIC_KEY = _get_license_setting("OFFLINE_PUBLIC_KEY")
LICENSE_SERVER_URL = _get_license_setting("LICENSE_SERVER_URL", "https://license.cerebion.com")

# CORS Configuration - restrict to specific origins for security
# In development: allows localhost
# In production: set ALLOWED_ORIGINS env var to your domains (comma-separated)
# Example: ALLOWED_ORIGINS=https://app.yourplatform.com,https://yourplatform.com
ALLOWED_ORIGINS_ENV = os.environ.get("ALLOWED_ORIGINS", "")
if ALLOWED_ORIGINS_ENV:
    # Production: use environment variable (comma-separated list)
    ALLOWED_ORIGINS = [origin.strip() for origin in ALLOWED_ORIGINS_ENV.split(",")]
else:
    # Development: allow common local development ports
    # Note: Electron "null" origin is handled by ElectronOriginMiddleware in app.py
    ALLOWED_ORIGINS = [
        "http://localhost:5173",  # Vite dev server
        "http://localhost:5174",  # Vite fallback port
        "http://localhost:3000",  # Alternative dev port
        "http://127.0.0.1:5173",
        "http://127.0.0.1:5174",
        "http://127.0.0.1:3000",
    ]

# AI Fix feature flags and settings
AI_FIX_MOCK_MODE = os.environ.get("AI_FIX_MOCK_MODE", "false").lower() in {"1", "true", "yes"}

# Fallback models — used only when the provider's live API cannot be reached.
# Override via environment variables if you need to pin to a specific model version.
AI_FALLBACK_MODEL_GOOGLE    = os.environ.get("AI_FALLBACK_MODEL_GOOGLE",    "gemini/gemini-flash-latest")
AI_FALLBACK_MODEL_ANTHROPIC = os.environ.get("AI_FALLBACK_MODEL_ANTHROPIC", "anthropic/claude-sonnet-4-6")
AI_FALLBACK_MODEL_OPENAI    = os.environ.get("AI_FALLBACK_MODEL_OPENAI",    "openai/gpt-4o")

# Code analysis rules configuration path (in same directory as this config file)
SCANNER_RULES_CONFIG_PATH = os.path.join(os.path.dirname(__file__), "code_analysis_rules.yaml")
