import os
import sys
# Windows consoles default to a legacy codepage (cp1252/cp850) that can't encode
# the arrows/checkmarks/emojis used throughout this codebase's print() calls —
# an uncaught UnicodeEncodeError on a background thread (e.g. the calendar sync
# loop) silently kills that task with no visible error. UTF-8 + replace makes
# every print() safe regardless of console codepage.
if sys.platform == "win32":
    for _stream in (sys.stdout, sys.stderr):
        try:
            _stream.reconfigure(encoding="utf-8", errors="replace")
        except Exception:
            pass
from pathlib import Path
from dotenv import load_dotenv
# Explicit path: don't rely on the process's current working directory (a systemd
# unit, pm2, or a launcher script may start uvicorn from somewhere other than
# backend/), otherwise .env silently fails to load and SAXO_APP_KEY/SECRET stay unset.
load_dotenv(dotenv_path=Path(__file__).resolve().parent / ".env")

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from routers import market_data, geopolitical, options, backtest, ai, portfolio, config, patterns, journal, cycle as cycle_router, profiles as profiles_router, reasoning as reasoning_router, knowledge as knowledge_router, options_vol as options_vol_router, analytics as analytics_router, risk as risk_router
from routers import pattern_lab as pattern_lab_router
from routers import specialist_desks as specialist_desks_router
from routers import timeline as timeline_router
from routers import instruments as instruments_router
from routers import impact as impact_router
from routers import cycle_actions as cycle_actions_router
from routers import market_events as market_events_router
from routers import ai_desks as ai_desks_router
from routers import logs as logs_router
from routers import var as var_router
from routers import reports as reports_router
from routers import institutional as institutional_router
from routers import eco as eco_router
from routers import simulator as simulator_router
from routers import causal_lab as causal_lab_router
from routers import instrument_models as instrument_models_router
from routers import instruments_watchlist as instruments_watchlist_router
from routers import wavelet as wavelet_router
from routers import ai_chat as ai_chat_router
from routers import strategy_builder as strategy_builder_router
from routers import saxo_oauth as saxo_oauth_router
from routers import saxo as saxo_router
from services.database import init_db, get_config, cleanup_stale_running_cycles
import logging
import uvicorn


class _DBLogHandler(logging.Handler):
    """Persist WARNING+ log records to system_logs table."""
    def emit(self, record: logging.LogRecord):
        if record.levelno < logging.WARNING:
            return
        try:
            from services.database import log_system_event
            log_system_event(
                level=record.levelname,
                source=record.name,
                message=self.format(record),
                cycle_id=getattr(record, "cycle_id", None),
                ticker=getattr(record, "ticker", None),
            )
        except Exception:
            pass  # Never raise from inside a log handler

app = FastAPI(
    title="OpenFin Intelligence",
    description="Geopolitical Options Trading Cockpit API",
    version="2.0.0",
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["http://localhost:5173", "http://localhost:3000", "http://127.0.0.1:5173"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.exception_handler(Exception)
async def _global_exception_handler(request, exc: Exception):
    """Catches anything that slips past a route's own try/except (including errors
    during response serialization, which happen after a handler already returned) —
    without this, such errors surface as an opaque bare "Internal Server Error" with
    no detail and nothing in System Logs. Every unhandled exception, anywhere in the
    app, now gets a full traceback in System Logs and a real JSON detail in the response."""
    import traceback
    from fastapi.responses import JSONResponse
    tb = traceback.format_exc()
    try:
        from services.database import log_system_event
        log_system_event(
            level="ERROR", source="unhandled_exception",
            message=f"{request.method} {request.url.path}: {exc}",
            details={"error": str(exc), "traceback": tb},
        )
    except Exception:
        pass  # Never let logging failure mask the original error
    return JSONResponse(status_code=500, content={"detail": f"Erreur interne: {exc} (voir System Logs)"})


@app.on_event("startup")
def startup():
    # Attach DB log handler to root logger (captures WARNING+ from all modules)
    _db_handler = _DBLogHandler()
    _db_handler.setFormatter(logging.Formatter("%(message)s"))
    logging.getLogger().addHandler(_db_handler)

    _log = logging.getLogger(__name__)

    init_db()

    # Clean up cycles that were left in 'running' state by a previous crashed process
    stale = cleanup_stale_running_cycles()
    if stale:
        _log.warning(f"[Startup] Cleaned up {stale} stale 'running' cycle(s) — server likely reloaded mid-cycle")

    # Sync OpenAI key: env var → DB (useful on fresh VPS deployments)
    key = get_config("openai_api_key") or ""
    if not key:
        env_key = os.environ.get("OPENAI_API_KEY", "")
        if env_key:
            from services.database import set_config
            set_config("openai_api_key", env_key)
            key = env_key
            _log.info("[Startup] OPENAI_API_KEY synced from environment to DB config")
    if key:
        os.environ["OPENAI_API_KEY"] = key
    # Seed built-in patterns into DB (idempotent)
    from services.geo_analyzer import GEO_PATTERNS
    from services.database import seed_builtin_patterns
    seed_builtin_patterns(GEO_PATTERNS)
    # Seed causal graph templates
    try:
        from services.database import get_conn as _get_conn
        from services.causal_graphs import init_tables as _cg_init, seed_templates as _cg_seed
        _cg_conn = _get_conn()
        _cg_init(_cg_conn); _cg_seed(_cg_conn); _cg_conn.close()
        _log.info("[Startup] Causal graph templates seeded")
    except Exception as _e:
        _log.warning(f"[Startup] Causal graph seed failed: {_e}")
    # Guidance sync au démarrage (crée les guidance events manquants)
    try:
        from services.guidance_sync import sync_guidance as _gs
        _gr = _gs()
        _log.info(f"[Startup] Guidance sync done: {_gr}")
    except Exception as _e:
        _log.warning(f"[Startup] Guidance sync failed: {_e}")
    # Seed instrument models (graphes causaux exhaustifs par instrument)
    try:
        from services.database import get_conn as _get_conn
        from services.instrument_models import seed_instrument_models as _sim
        _im_conn = _get_conn()
        _sim(_im_conn); _im_conn.close()
        _log.info("[Startup] Instrument models seeded")
    except Exception as _e:
        _log.warning(f"[Startup] Instrument models seed failed: {_e}")
    # Backfill wavelet_engine/extremum/level_threshold defaults onto the Technical
    # Desk so the AI Desks toggle UI matches what's actually computed each cycle
    try:
        from services.database import backfill_wavelet_desk_defaults
        backfill_wavelet_desk_defaults()
        _log.info("[Startup] Wavelet desk defaults backfilled")
    except Exception as _e:
        _log.warning(f"[Startup] Wavelet desk defaults backfill failed: {_e}")
    # Auto-bootstrap désactivé — utiliser les boutons dans Cycle Actions / Timeline
    # Start auto-cycle scheduler if enabled
    from services.auto_cycle import start_scheduler
    start_scheduler()
    # Start VaR + PnL snapshot schedulers
    from services.var_scheduler import start_var_scheduler, start_pnl_scheduler
    start_var_scheduler()
    start_pnl_scheduler()
    # Start institutional reports weekly scheduler (COT Fridays, EIA Wednesdays)
    from services.institutional_scheduler import start_institutional_scheduler
    start_institutional_scheduler()
    # Start Saxo OAuth token refresh + options-chain snapshot poller
    from services.saxo_scheduler import start_saxo_scheduler
    start_saxo_scheduler()
    # Start Saxo-priced Watchlist + wavelet recompute refresh (own cadence, independent of
    # the once-a-day auto_cycle — see services/wavelet_scheduler.py)
    from services.wavelet_scheduler import start_wavelet_scheduler
    start_wavelet_scheduler()

    # One-time cleanup: collapse snapshot rows stored before save-time dedup existed
    try:
        from services.database import dedupe_saxo_snapshots
        deleted = dedupe_saxo_snapshots()
        if deleted:
            _log.info(f"[Saxo] Startup cleanup: removed {deleted} redundant snapshot rows")
    except Exception as e:
        _log.warning(f"[Saxo] Startup snapshot dedupe failed: {e}")

    # One-time migration: remove FXStreet-sourced rows (decimal format) — FF sync below will refill from FF
    try:
        from services.database import get_conn as _get_conn
        _mc = _get_conn()
        _deleted = _mc.execute("DELETE FROM ff_calendar WHERE source='fxstreet'").rowcount
        _mc.commit()
        _mc.close()
        if _deleted:
            print(f"[Startup] Purged {_deleted} FXStreet rows — will refill from FF on next calendar sync", flush=True)
    except Exception as _e:
        print(f"[Startup] FXStreet purge skipped: {_e}", flush=True)

    # Calendar sync loop — FF live JSON every calendar_refresh_h (default 6h); the FF HTML
    # scraper (heavier, one FlareSolverr browser fetch per week) is throttled separately
    # inside calendar_sync() via calendar_scrape_refresh_h (default 24h, once/day)
    import threading, time as _time
    def _calendar_sync_loop():
        _time.sleep(60)  # let server fully boot
        while True:
            try:
                from datetime import datetime as _dt, timezone as _tz, timedelta as _td
                interval_h = float(get_config("calendar_refresh_h") or 6)
                from services.calendar_sync import calendar_sync, set_next_run
                set_next_run((_dt.now(_tz.utc) + _td(hours=interval_h)).isoformat())
                r = calendar_sync()
                src = r.get("source") or ("ff_fallback" if r.get("_fallback") else "fxstreet")
                print(f"[Calendar] Sync done — {src}, upserted={r.get('total_upserted', '?')}", flush=True)
                # Guidance sync — crée/met à jour les guidance events après chaque calendar sync
                try:
                    from services.guidance_sync import sync_guidance
                    gr = sync_guidance()
                    print(f"[Guidance] Sync done — {gr}", flush=True)
                except Exception as _ge:
                    print(f"[Guidance] Sync error: {_ge}", flush=True)
            except Exception as e:
                print(f"[Calendar] Sync loop error: {e}", flush=True)
            try:
                interval_h = float(get_config("calendar_refresh_h") or 6)
            except Exception:
                interval_h = 6
            _time.sleep(int(interval_h * 3600))

    threading.Thread(target=_calendar_sync_loop, daemon=True).start()

    # Auto-import Forex Factory CSV if file is present (force, then delete CSV)
    import threading
    def _auto_import_ff():
        import os
        from routers.eco import _find_csv, _run_ff_import, _ff_import_status
        csv_path = _find_csv()
        if not csv_path:
            print("[Startup] forex_factory_cache.csv not found — skipping FF auto-import", flush=True)
            return
        print(f"[Startup] FF CSV found at {csv_path} — importing (force) …", flush=True)
        _run_ff_import(csv_path)
        result = _ff_import_status.get("last_result") or {}
        inserted = result.get("inserted", 0)
        total   = result.get("total", 0)
        print(f"[Startup] FF import done — {inserted} inserted / {total} total", flush=True)
        # Delete CSV after successful import so next restart skips automatically
        try:
            os.remove(csv_path)
            print(f"[Startup] CSV deleted: {csv_path}", flush=True)
        except Exception as e:
            print(f"[Startup] Could not delete CSV: {e}", flush=True)

    threading.Thread(target=_auto_import_ff, daemon=True).start()


@app.on_event("shutdown")
def shutdown():
    from services.auto_cycle import stop_scheduler
    stop_scheduler()
    from services.var_scheduler import stop_var_scheduler, stop_pnl_scheduler
    stop_var_scheduler()
    stop_pnl_scheduler()
    from services.institutional_scheduler import stop_institutional_scheduler
    stop_institutional_scheduler()
    from services.saxo_scheduler import stop_saxo_scheduler
    stop_saxo_scheduler()
    from services.wavelet_scheduler import stop_wavelet_scheduler
    stop_wavelet_scheduler()


app.include_router(market_data.router)
app.include_router(geopolitical.router)
app.include_router(options.router)
app.include_router(backtest.router)
app.include_router(ai.router)
app.include_router(portfolio.router)
app.include_router(config.router)
app.include_router(patterns.router)
app.include_router(journal.router)
app.include_router(cycle_router.router)
app.include_router(profiles_router.router)
app.include_router(reasoning_router.router)
app.include_router(knowledge_router.router)
app.include_router(options_vol_router.router)
app.include_router(analytics_router.router)
app.include_router(risk_router.router)
app.include_router(logs_router.router)
app.include_router(var_router.router)
app.include_router(reports_router.router)
app.include_router(institutional_router.router)
app.include_router(pattern_lab_router.router)
app.include_router(specialist_desks_router.router)
app.include_router(timeline_router.router)
app.include_router(instruments_router.router)
app.include_router(impact_router.router)
app.include_router(cycle_actions_router.router)
app.include_router(market_events_router.router)
app.include_router(ai_desks_router.router)
app.include_router(eco_router.router)
app.include_router(simulator_router.router)
app.include_router(causal_lab_router.router)
app.include_router(instrument_models_router.router)
app.include_router(instruments_watchlist_router.router)
app.include_router(wavelet_router.router)
app.include_router(ai_chat_router.router)
app.include_router(strategy_builder_router.router)
app.include_router(saxo_oauth_router.router)
app.include_router(saxo_router.router)


@app.get("/")
def root():
    return {"app": "OpenFin Intelligence Cockpit", "version": "2.0.0", "docs": "/docs"}


@app.get("/api/health")
def health():
    return {"status": "ok", "version": "2.0.0"}


if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
