"""8 triangulation-targeted follow-up design cards."""
from __future__ import annotations

from typing import Dict, List, Optional

from .bias import diagnose_discordance


DESIGN_CARDS_BASE: List[Dict[str, object]] = [
    {
        "type": "MVMR PNPLA3+BMI+T2DM",
        "rationale": "MASLD-mediated vs metabolic-confounder 분리. PNPLA3(간특이)+BMI+T2DM 동시 IV.",
        "primary_endpoint": "log(OR) outcome",
        "sample_size": "UKB ≥300k + KoBB ≥70k",
        "follow_up": "GWAS summary stats only",
        "key_hypothesis": "Liver-specific log-effect > metabolic confounder log-effect 이면 MASLD 인과.",
        "mr_instruments_required": ["PNPLA3", "BMI_polygenic", "T2DM_polygenic"],
        "genotype_stratification": False,
        "tier": "primary",
    },
    {
        "type": "PCLS genotyped (precision-cut liver slices)",
        "rationale": "PNPLA3 GG vs CC liver slice 에서 약물 ex vivo 효과 비교 — TM6SF2 carrier 추가.",
        "primary_endpoint": "Fibrogenesis (COL1A1·αSMA) + lipid droplet area",
        "sample_size": "n=30 donors (PNPLA3 GG 10 / CG 10 / CC 10)",
        "follow_up": "72h ex vivo",
        "key_hypothesis": "PNPLA3 GG slice 에서 resmetirom 더 큰 lipid clearance.",
        "mr_instruments_required": ["PNPLA3", "TM6SF2"],
        "genotype_stratification": True,
        "tier": "mechanism",
    },
    {
        "type": "Active comparator new-user (target trial emulation)",
        "rationale": "Resmetirom vs vitamin E new-user, KoNEHS/NHIS DB, 4-year follow-up.",
        "primary_endpoint": "LRC mortality + decompensation composite",
        "sample_size": "n≥8000 propensity-matched",
        "follow_up": "4 years",
        "key_hypothesis": "Active comparator design은 confounding-by-indication 감소.",
        "mr_instruments_required": [],
        "genotype_stratification": False,
        "tier": "real_world",
    },
    {
        "type": "Genotype-stratified RCT subgroup (PNPLA3·HSD17B13·TM6SF2)",
        "rationale": "MAESTRO-NASH·ESSENCE·SYNERGY-NASH 사후 genotype subgroup.",
        "primary_endpoint": "Histology resolution + fibrosis stage 개선",
        "sample_size": "n≥1500 (각 RCT pooled)",
        "follow_up": "52 weeks",
        "key_hypothesis": "PNPLA3 GG subgroup: 더 큰 resmetirom 효과. TM6SF2: CV outcome 분리.",
        "mr_instruments_required": ["PNPLA3", "HSD17B13", "TM6SF2"],
        "genotype_stratification": True,
        "tier": "rct_subgroup",
    },
    {
        "type": "Within-subject lifestyle crossover (PNPLA3 stratified)",
        "rationale": "체중 5-10% 감소 → re-gain → 재감소 시 MASH/MR-PDFF 회복 추적.",
        "primary_endpoint": "MR-PDFF Δ + FIB-4 Δ",
        "sample_size": "n=120 (PNPLA3 GG 40, CG 40, CC 40)",
        "follow_up": "12 months",
        "key_hypothesis": "PNPLA3 GG carrier 에서 체중 회복 시 MASH 더 빠르게 재발.",
        "mr_instruments_required": ["PNPLA3"],
        "genotype_stratification": True,
        "tier": "within_subject",
    },
    {
        "type": "Negative control outcome / exposure",
        "rationale": "MASLD → 골절 (생물학적 plausible 약함) 등 음성대조로 residual confounding 탐지.",
        "primary_endpoint": "Negative control HR (≈1 기대)",
        "sample_size": "Same cohort",
        "follow_up": "Same",
        "key_hypothesis": "음성대조 HR≠1 이면 residual confounding 존재.",
        "mr_instruments_required": [],
        "genotype_stratification": False,
        "tier": "sensitivity",
    },
    {
        "type": "Bridging animal → human (DIO + PCLS + UKB)",
        "rationale": "DIO mouse → human PCLS → UKB MR 통합 effect estimate.",
        "primary_endpoint": "Cross-species standardized effect (log-scale)",
        "sample_size": "Mouse n=40 + PCLS n=20 + UKB n≥300k",
        "follow_up": "Variable",
        "key_hypothesis": "동물·ex vivo·인간 효과 방향 일치 시 인과 강화.",
        "mr_instruments_required": ["PNPLA3"],
        "genotype_stratification": False,
        "tier": "translational",
    },
    {
        "type": "Bariatric surgery MASLD natural experiment",
        "rationale": "비만수술 후 빠른 MASLD 회복 — instrumental variable (surgery date).",
        "primary_endpoint": "Pre-post MR-PDFF / FibroScan",
        "sample_size": "n≥1500 Korean bariatric registry",
        "follow_up": "24 months",
        "key_hypothesis": "MASLD 회복 후 outcome (CV·CKD·HCC) 감소 시 MASLD 인과 강화.",
        "mr_instruments_required": [],
        "genotype_stratification": False,
        "tier": "natural_experiment",
    },
]


def proposed_designs(
    effects: Optional[List[Dict[str, object]]] = None,
    stage: Optional[str] = None,
    outcome: Optional[str] = None,
) -> Dict[str, object]:
    """Return all 8 design cards, optionally annotated with discordance context."""
    cards = [dict(c) for c in DESIGN_CARDS_BASE]
    context = None
    if effects is not None and stage and outcome:
        context = diagnose_discordance(effects, stage, outcome)
        flag_codes = context.get("taxonomy_codes_referenced", [])
        # mark relevance per card
        relevance_map = {
            "MVMR PNPLA3+BMI+T2DM": ["metabolic_syndrome_confounding", "shared_genetic_confounder"],
            "PCLS genotyped (precision-cut liver slices)": ["mr_pleiotropy", "canalization"],
            "Active comparator new-user (target trial emulation)":
                ["metabolic_syndrome_confounding", "selection_bias_biopsy"],
            "Genotype-stratified RCT subgroup (PNPLA3·HSD17B13·TM6SF2)":
                ["mr_pleiotropy", "canalization", "dose_translation"],
            "Within-subject lifestyle crossover (PNPLA3 stratified)":
                ["reverse_causation", "metabolic_syndrome_confounding"],
            "Negative control outcome / exposure": ["metabolic_syndrome_confounding", "detection_bias_nit"],
            "Bridging animal → human (DIO + PCLS + UKB)": ["dose_translation", "canalization"],
            "Bariatric surgery MASLD natural experiment":
                ["reverse_causation", "external_validity_korean"],
        }
        for c in cards:
            rel = set(relevance_map.get(c["type"], [])) & set(flag_codes)
            c["relevance_to_current_pair"] = sorted(rel) if rel else []
            c["relevance_score"] = len(rel)
        cards.sort(key=lambda c: -c.get("relevance_score", 0))

    return {
        "n_cards": len(cards),
        "cards": cards,
        "discordance_context": context,
    }
