"""
Bootstrap a curated set of historical macro + geopolitical events into market_events.
These events carry keywords in their names/descriptions that match instrument event_keywords,
so they will appear in instrument snapshots and the timeline driver strip.
"""
import logging
from typing import List, Dict, Any

logger = logging.getLogger(__name__)

# ── Predefined events list ─────────────────────────────────────────────────────
# Each event: name, start_date, end_date, level, category, description,
#             affected_assets (JSON-like string), impact_score

MACRO_EVENTS: List[Dict[str, Any]] = [
    # ── Fed / FOMC ────────────────────────────────────────────────────────────
    {
        "name": "FOMC — Hausse taux +25pb (début cycle de resserrement)",
        "start_date": "2022-03-16", "end_date": None,
        "level": "long", "category": "event_calendar", "sub_type": "FOMC",
        "description": "Fed commence son cycle de hausse de taux pour combattre l'inflation record. Premier FOMC hawkish depuis 2018. Dollar Index (DXY) s'apprécie fortement. Pression sur les taux réels et les valeurs de croissance.",
        "affected_assets": '["SPY","QQQ","TLT","GLD","USDJPY=X","BTC-USD"]', "impact_score": 0.85,
    },
    {
        "name": "FOMC — Hausse +50pb (accélération resserrement)",
        "start_date": "2022-05-04", "end_date": None,
        "level": "medium", "category": "event_calendar", "sub_type": "FOMC",
        "description": "Fed accélère avec +50pb, le plus fort depuis 2000. Taux cibles 0.75-1.00%. CPI à 8.3% en avril. Signal fort de la Fed pour tuer l'inflation quoi qu'il en coûte.",
        "affected_assets": '["SPY","QQQ","TLT","GLD","IWM"]', "impact_score": 0.80,
    },
    {
        "name": "FOMC — Choc hawkish +75pb (premier 3/4 de point depuis 1994)",
        "start_date": "2022-06-15", "end_date": None,
        "level": "long", "category": "event_calendar", "sub_type": "FOMC",
        "description": "FOMC surprise +75pb, le plus fort depuis 1994. CPI a 9.1% en juin (record 40 ans). Taux cibles 1.50-1.75%. Début du vrai bear market sur les indices. Volatilité VIX explose.",
        "affected_assets": '["SPY","QQQ","TLT","GLD","VXX","USDJPY=X","IWM"]', "impact_score": 0.95,
    },
    {
        "name": "FOMC — Hausse +75pb (4e hausse consécutive hawkish)",
        "start_date": "2022-09-21", "end_date": None,
        "level": "medium", "category": "event_calendar", "sub_type": "FOMC",
        "description": "4e hausse consécutive de +75pb. Dot plot révisé fortement à la hausse. Taux terminal attendu à 4.6% en 2023. Dollar au plus haut depuis 20 ans. Pression extrême sur toutes les classes d'actifs.",
        "affected_assets": '["SPY","QQQ","TLT","GLD","EURUSD=X","IWM","HYG"]', "impact_score": 0.90,
    },
    {
        "name": "FOMC — Hausse +50pb (décélération, pivot en vue)",
        "start_date": "2022-12-14", "end_date": None,
        "level": "medium", "category": "event_calendar", "sub_type": "FOMC",
        "description": "Première décélération à +50pb après 4x+75pb. Taux 4.25-4.50%. Dot plot révèle taux terminal de 5.1% pour 2023. Signal que le pic de la vitesse d'augmentation est passé.",
        "affected_assets": '["SPY","QQQ","TLT","GLD","USDJPY=X"]', "impact_score": 0.80,
    },
    {
        "name": "FOMC — Pause taux (pic du cycle à 5.25-5.50%)",
        "start_date": "2023-06-14", "end_date": None,
        "level": "long", "category": "event_calendar", "sub_type": "FOMC",
        "description": "FOMC marque une pause après 10 hausses consécutives. Taux directeur au pic du cycle à 5.25-5.50%. Début de la période de 'higher for longer'. Marché commence à pricer les baisses futures.",
        "affected_assets": '["SPY","QQQ","TLT","GLD","HYG"]', "impact_score": 0.80,
    },
    {
        "name": "FOMC — Pivot Fed : première baisse de taux -50pb",
        "start_date": "2024-09-18", "end_date": None,
        "level": "long", "category": "event_calendar", "sub_type": "FOMC",
        "description": "Pivot majeur de la Fed : première baisse de taux depuis 2020, -50pb surprend les marchés (consensus attendait -25pb). Taux 4.75-5.00%. Début du cycle d'assouplissement. Signal bullish pour actions, gold, crypto.",
        "affected_assets": '["SPY","QQQ","GLD","TLT","BTC-USD","IWM","HYG"]', "impact_score": 0.90,
    },
    {
        "name": "FOMC — Dot plot hawkish : révision à la baisse des baisses 2025",
        "start_date": "2024-12-18", "end_date": None,
        "level": "medium", "category": "event_calendar", "sub_type": "FOMC",
        "description": "FOMC baisse taux -25pb mais dot plot révise de 4 à 2 baisses prévues en 2025. Ton hawkish surprend. Dollar fort, taux longs remontent. Correction de fin d'année sur les indices.",
        "affected_assets": '["SPY","QQQ","TLT","GLD","USDJPY=X"]', "impact_score": 0.85,
    },
    {
        "name": "FOMC — Pause prolongée (incertitude tarifaire Trump)",
        "start_date": "2025-03-19", "end_date": "2025-06-30",
        "level": "medium", "category": "event_calendar", "sub_type": "FOMC",
        "description": "Fed en pause prolongée face à l'incertitude créée par les tarifs douaniers Trump. CPI autour de 2.8%. Fed attend de voir l'impact inflationniste des tarifs avant de baisser à nouveau.",
        "affected_assets": '["SPY","QQQ","TLT","GLD","EURUSD=X"]', "impact_score": 0.75,
    },

    # ── CPI / Inflation ───────────────────────────────────────────────────────
    {
        "name": "CPI Juin 2022 — Pic inflationniste 9.1% (sommet 40 ans)",
        "start_date": "2022-06-10", "end_date": None,
        "level": "long", "category": "event_calendar", "sub_type": "CPI",
        "description": "CPI US atteint 9.1% sur un an, le plus haut depuis 40 ans. Énergie +41.6%, alimentation +10.4%. Oblige la Fed à agir de façon agressive. Choc majeur pour TLT et valeurs de croissance. GLD paradoxalement vendus (dollar fort).",
        "affected_assets": '["TLT","SPY","QQQ","GLD","EURUSD=X"]', "impact_score": 0.95,
    },
    {
        "name": "CPI Novembre 2022 — Inflexion à la baisse (début désinflation)",
        "start_date": "2022-11-10", "end_date": None,
        "level": "long", "category": "event_calendar", "sub_type": "CPI",
        "description": "CPI 7.7%, sous les 8% attendus pour la première fois depuis plusieurs mois. Signal d'inflexion décisif. Méga-rally intraday sur les indices (+5%), or, crypto. Dollar chute fortement. Début du discours de désinflation.",
        "affected_assets": '["SPY","QQQ","GLD","BTC-USD","TLT","EURUSD=X"]', "impact_score": 0.90,
    },
    {
        "name": "CPI Mars 2024 — Surprise haussière 3.5% (recul désinflation)",
        "start_date": "2024-04-10", "end_date": None,
        "level": "medium", "category": "event_calendar", "sub_type": "CPI",
        "description": "CPI 3.5% sur un an, au-dessus des attentes. Reporte les anticipations de baisses Fed. Taux 10Y remontent vers 4.5%. Correction sur TLT et valeurs de croissance. GLD résistant grâce aux achats de banques centrales.",
        "affected_assets": '["TLT","QQQ","SPY","USDJPY=X"]', "impact_score": 0.80,
    },

    # ── Géopolitique ──────────────────────────────────────────────────────────
    {
        "name": "Invasion russe de l'Ukraine — Choc géopolitique majeur",
        "start_date": "2022-02-24", "end_date": "2023-02-24",
        "level": "long", "category": "geopolitical",
        "description": "Russie envahit l'Ukraine. Choc géopolitique majeur. Pétrole Brent +30% en semaines. Gaz naturel européen x5. Gold +5%. Dollar fort. Sanctions massives contre Russie. Crise énergétique en Europe.",
        "affected_assets": '["GLD","USO","UNG","EURUSD=X","EFA","VXX","SLV"]', "impact_score": 0.98,
    },
    {
        "name": "Attaque Hamas — Israël (Opération Déluge Al-Aqsa)",
        "start_date": "2023-10-07", "end_date": "2024-06-30",
        "level": "long", "category": "geopolitical",
        "description": "Attaque terroriste du Hamas sur Israël — 1200 morts. Début guerre Gaza. Crainte d'escalade régionale impliquant l'Iran. Prime de risque géopolitique sur pétrole, gold, VIX. Moyen-Orient en tension maximale.",
        "affected_assets": '["GLD","USO","VXX","SLV"]', "impact_score": 0.90,
    },
    {
        "name": "Iran vs Israël — Attaque directe (drones + missiles)",
        "start_date": "2024-04-13", "end_date": None,
        "level": "medium", "category": "geopolitical",
        "description": "Iran lance 300+ drones et missiles sur Israël — première attaque directe de l'Iran. Israel intercepte ~99%. Crainte d'escalade régionale. Pétrole +3%, Gold +2%, VIX +15%. Finalement absorbé rapidement.",
        "affected_assets": '["GLD","USO","VXX","EURUSD=X"]', "impact_score": 0.85,
    },
    {
        "name": "Donald Trump — Retour à la Maison Blanche (tarifs douaniers)",
        "start_date": "2025-01-20", "end_date": "2025-12-31",
        "level": "long", "category": "geopolitical",
        "description": "Investiture de Donald Trump. Annonce de tarifs douaniers massifs (10-25% sur Chine, 25% Canada/Mexique). Guerre commerciale mondiale. Dollar fort, dollar canadien/mexicain sous pression. Inflation importée attendue.",
        "affected_assets": '["SPY","QQQ","EEM","EURUSD=X","GBPUSD=X","GLD","BTC-USD","USO"]', "impact_score": 0.90,
    },
    {
        "name": "Choc tarifaire Trump — Liberation Day (tarifs réciproques)",
        "start_date": "2025-04-02", "end_date": "2025-04-09",
        "level": "medium", "category": "geopolitical",
        "description": "Trump annonce tarifs réciproques massifs le 2 avril. SPY -5% sur la semaine. Crainte de récession + inflation. HYG spreads élargissent. TLT flight-to-quality. Gold résistant. Pause annoncée 9 avril (sauf Chine).",
        "affected_assets": '["SPY","QQQ","IWM","TLT","VXX","GLD","EURUSD=X"]', "impact_score": 0.92,
    },

    # ── OPEC / Énergie ────────────────────────────────────────────────────────
    {
        "name": "OPEP+ — Réduction surprise production -1.16M b/j",
        "start_date": "2023-04-02", "end_date": None,
        "level": "medium", "category": "event_calendar", "sub_type": "OPEC+",
        "description": "OPEP+ annonce réduction volontaire surprise de 1.16M barils/jour. Brent +8% en une séance. Crainte de relance inflationniste. Profite à XOM, ExxonMobil et à GLD (inflation). Mauvaise surprise pour les banques centrales.",
        "affected_assets": '["USO","XOM","GLD","EURUSD=X","SPY"]', "impact_score": 0.85,
    },
    {
        "name": "OPEP+ — Extension coupes production jusqu'à fin 2025",
        "start_date": "2024-11-05", "end_date": "2025-12-31",
        "level": "long", "category": "event_calendar", "sub_type": "OPEC+",
        "description": "OPEP+ annonce extension des coupes de production jusqu'à fin 2025. Pétrole soutenu autour de 70-80$. Soutien pour les majors pétrolières. Impact modéré car compensé par hausse production US (schiste).",
        "affected_assets": '["USO","XOM","GLD","EEM"]', "impact_score": 0.70,
    },

    # ── BOJ / Yen ─────────────────────────────────────────────────────────────
    {
        "name": "BOJ — Surprise élargissement YCC ±50pb (début fin des taux négatifs)",
        "start_date": "2022-12-20", "end_date": None,
        "level": "medium", "category": "event_calendar", "sub_type": "BOJ",
        "description": "BOJ surprend le marché en élargissant la bande de contrôle des taux (YCC) de ±25pb à ±50pb. Premier signe de la fin de la politique ultra-accommodante. Yen +3% instantanément. USD/JPY chute de 137 à 131.",
        "affected_assets": '["USDJPY=X","TLT","GLD","EFA"]', "impact_score": 0.85,
    },
    {
        "name": "BOJ — Fin des taux négatifs (ZIRP terminé)",
        "start_date": "2024-03-19", "end_date": None,
        "level": "long", "category": "event_calendar", "sub_type": "BOJ",
        "description": "BOJ relève ses taux pour la première fois depuis 2007. Fin de la politique des taux négatifs (ZIRP). Taux 0-0.1%. Signal historique pour le yen. USD/JPY perd 1.5%. Carry trade commence à se dénouer progressivement.",
        "affected_assets": '["USDJPY=X","TLT","GLD","EFA","EURUSD=X"]', "impact_score": 0.88,
    },
    {
        "name": "BOJ — Hausse surprise +15pb (débâcle carry trade Yen)",
        "start_date": "2024-07-31", "end_date": "2024-08-05",
        "level": "medium", "category": "event_calendar", "sub_type": "BOJ",
        "description": "BOJ hausse ses taux à 0.25% surprise. Yen +7% en 48h. USD/JPY 161 → 141. Débâcle du carry trade yen: VIX spike à 65, SPY -8%, Nikkei -12%. Crise de liquidité brève mais intense. La plus forte correction des marchés de 2024.",
        "affected_assets": '["USDJPY=X","SPY","QQQ","VXX","GLD","EFA"]', "impact_score": 0.92,
    },

    # ── IA / Tech ─────────────────────────────────────────────────────────────
    {
        "name": "Lancement ChatGPT — Révolution IA générative (OpenAI)",
        "start_date": "2022-11-30", "end_date": "2023-06-30",
        "level": "long", "category": "fundamental", "sub_type": "Tech/IA",
        "description": "OpenAI lance ChatGPT. 1 million d'utilisateurs en 5 jours. Début de la révolution IA générative. Reclassification de NVIDIA comme infrastructure critique. Accélération des investissements capex IA des hyperscalers.",
        "affected_assets": '["NVDA","QQQ","AAPL","SPY"]', "impact_score": 0.95,
    },
    {
        "name": "NVIDIA Earnings — Guidance blow-out AI (H100 en pénurie)",
        "start_date": "2023-05-24", "end_date": None,
        "level": "medium", "category": "report", "sub_type": "Earnings",
        "description": "NVIDIA guide pour 11Md$ au Q2 2023 vs 7.2Md$ attendu. Catalyseur IA : la demande GPU H100 explose. NVDA +25% en une séance. Début du mega bull run IA. Entraîne QQQ et les valeurs tech.",
        "affected_assets": '["NVDA","QQQ","AAPL","SPY"]', "impact_score": 0.88,
    },
    {
        "name": "NVIDIA — Passage en trillion de dollars (capex IA hyperscalers)",
        "start_date": "2024-06-05", "end_date": None,
        "level": "medium", "category": "report", "sub_type": "Earnings",
        "description": "NVIDIA earnings record : revenus data center +427% YoY. L'IA capex des hyperscalers (Amazon, Microsoft, Google) explose. NVDA dépasse Apple brièvement en capitalisation. Confirmation du supercycle IA.",
        "affected_assets": '["NVDA","QQQ","AAPL","SPY"]', "impact_score": 0.85,
    },

    # ── Crypto ────────────────────────────────────────────────────────────────
    {
        "name": "Approbation ETF Bitcoin spot US (BlackRock, Fidelity)",
        "start_date": "2024-01-10", "end_date": None,
        "level": "long", "category": "fundamental", "sub_type": "Crypto",
        "description": "SEC approuve 11 ETF Bitcoin spot dont BlackRock (IBIT) et Fidelity (FBTC). Événement historique pour l'adoption institutionnelle du Bitcoin. BTC +20% en 2 semaines. Afflux de capitaux via ETF : >10Md$ en 1 mois.",
        "affected_assets": '["BTC-USD","SPY"]', "impact_score": 0.92,
    },
    {
        "name": "Halving Bitcoin 2024 — Réduction récompense de bloc 50%",
        "start_date": "2024-04-20", "end_date": None,
        "level": "medium", "category": "fundamental", "sub_type": "Crypto",
        "description": "4e halving Bitcoin : récompense mineurs passe de 6.25 à 3.125 BTC. Offre réduite de 50%. Catalyseur historique de bull markets. Combiné aux ETF, pression acheteuse sur BTC s'intensifie.",
        "affected_assets": '["BTC-USD"]', "impact_score": 0.80,
    },

    # ── Crise bancaire ────────────────────────────────────────────────────────
    {
        "name": "Effondrement Silicon Valley Bank (SVB) — Crise bancaire US",
        "start_date": "2023-03-10", "end_date": "2023-03-31",
        "level": "medium", "category": "geopolitical",
        "description": "SVB fait faillite, la 2e plus grande banque US à s'effondrer. Panique bancaire régionale. Crédit Suisse sous pression (repris par UBS). Fed crée BTFP pour fournir liquidités. IWM et HYG sous pression. GLD refuge.",
        "affected_assets": '["GLD","IWM","HYG","VXX","TLT","SPY"]', "impact_score": 0.88,
    },

    # ── Chine / EM ────────────────────────────────────────────────────────────
    {
        "name": "Xi Jinping — 3e mandat historique (Congrès PCC)",
        "start_date": "2022-10-16", "end_date": None,
        "level": "long", "category": "geopolitical",
        "description": "Congrès du PCC confirme Xi Jinping pour un 3e mandat sans précédent. Consolidation du pouvoir. Inquiétudes sur l'économie Chine, immobilier (Evergrande), politique Zéro Covid. EEM sous forte pression. Dollar Offshore CNH affaibli.",
        "affected_assets": '["EEM","AAPL","QQQ","EURUSD=X"]', "impact_score": 0.80,
    },
    {
        "name": "Stimulus Chine — PBOC annonce relance économique massive",
        "start_date": "2024-09-24", "end_date": "2024-10-31",
        "level": "medium", "category": "fundamental", "sub_type": "PBOC",
        "description": "PBOC annonce un package de relance économique surprise : baisse RRR, soutien immobilier, liquidités injectées. EEM +15% en 10 jours. Cuivre, pétrole, matières premières en hausse. Signal de redémarrage Chine.",
        "affected_assets": '["EEM","USO","GLD","SLV","EURUSD=X"]', "impact_score": 0.85,
    },
]


def bootstrap_macro_events(force: bool = False) -> Dict[str, Any]:
    """
    Insert the curated macro events into market_events table.
    Skips events whose name already exists in the DB (idempotent).
    Pass force=True to wipe existing bootstrapped events and re-insert.
    Returns: {inserted, skipped, total}
    """
    from services.database import get_all_market_events, save_market_event

    # Build set of existing event names for deduplication
    existing_events = get_all_market_events()
    existing_names = {ev.get("name", "") for ev in existing_events}

    if force:
        # Delete all existing events that match our bootstrap names
        from services.database import delete_market_event
        deleted = 0
        for ev in existing_events:
            if ev.get("name") in {e["name"] for e in MACRO_EVENTS}:
                delete_market_event(ev["id"])
                deleted += 1
        existing_names = set()
        logger.info(f"[macro_bootstrap] Force=True: deleted {deleted} existing events")

    inserted = 0
    skipped = 0
    errors = 0
    import json
    for ev in MACRO_EVENTS:
        if ev["name"] in existing_names:
            skipped += 1
            continue
        try:
            assets_raw = ev.get("affected_assets", "[]")
            assets_list = json.loads(assets_raw) if isinstance(assets_raw, str) else assets_raw
            save_market_event({
                "name": ev["name"],
                "start_date": ev["start_date"],
                "end_date": ev.get("end_date"),
                "level": ev["level"],
                "category": ev["category"],
                "description": ev["description"],
                "market_impact": ev["description"][:80],
                "affected_assets": assets_list,
                "impact_score": ev.get("impact_score", 0.75),
                "origin": "bootstrap_macro",
            })
            inserted += 1
        except Exception as e:
            logger.warning(f"[macro_bootstrap] Failed to insert {ev['name'][:40]}: {e}")
            errors += 1

    from services.database import count_market_events
    total = count_market_events()
    logger.info(f"[macro_bootstrap] Inserted {inserted}, skipped {skipped}, {errors} errors. Total: {total}")
    return {"inserted": inserted, "skipped": skipped, "errors": errors, "total": total}
