from __future__ import annotations

from grantflow.api import runtime_state


def seed_portfolio_quality_jobs() -> str:
    donor = "portfolio_quality_signal_test_donor"
    runtime_state.JOB_STORE.set(
        "portfolio-quality-job-1",
        {
            "status": "done",
            "hitl_enabled": True,
            "generate_preflight": {"warning_level": "medium", "risk_level": "medium"},
            "state": {
                "donor_id": donor,
                "quality_score": 8.5,
                "critic_score": 8.0,
                "needs_revision": True,
                "logframe_draft": {
                    "indicators": [
                        {
                            "indicator_id": "IND_001",
                            "name": "Civil servants completing AI policy training",
                            "baseline": "0 people",
                            "target": "300 people",
                            "frequency": "quarterly",
                            "formula": "Count of certified participants",
                            "definition": "Number of participants passing post-training assessment.",
                            "data_source": "Training attendance records",
                            "disaggregation": ["sex", "region"],
                            "result_level": "outcome",
                        },
                        {
                            "indicator_id": "IND_002",
                            "name": "Government SOPs updated for AI governance",
                            "baseline": "TBD",
                            "target": "",
                            "frequency": "semiannual",
                            "data_source": "Official SOP approval registry",
                            "result_level": "output",
                        },
                    ]
                },
                "toc_validation": {"valid": True, "schema_name": "UsaidTocSchema"},
                "toc_generation_meta": {
                    "engine": "fallback:contract_synthesizer",
                    "citation_policy": {"threshold_mode": "donor_section"},
                },
                "architect_retrieval": {"enabled": True, "hits_count": 2, "namespace": "usaid_ads201"},
                "critic_notes": {
                    "fatal_flaws": [
                        {
                            "finding_id": "f1",
                            "severity": "high",
                            "status": "open",
                            "section": "toc",
                            "code": "CAUSAL_LINK_DETAIL",
                            "message": "Causal links are underspecified for core pathway.",
                            "source": "llm",
                            "label": "CAUSAL_LINK_DETAIL",
                        },
                        {
                            "finding_id": "f2",
                            "severity": "low",
                            "status": "resolved",
                            "section": "general",
                            "code": "GENERAL_NOTE",
                            "message": "Minor editorial improvement.",
                        },
                    ],
                    "rule_checks": [
                        {"code": "toc.complete", "status": "fail"},
                        {"code": "toc.assumptions", "status": "warn"},
                    ],
                    "llm_advisory_diagnostics": {
                        "advisory_applies": True,
                        "advisory_candidate_count": 1,
                        "candidate_label_counts": {"CAUSAL_LINK_DETAIL": 1},
                    },
                },
                "citations": [
                    {
                        "stage": "architect",
                        "citation_type": "rag_claim_support",
                        "citation_confidence": 0.9,
                        "confidence_threshold": 0.7,
                    },
                    {
                        "stage": "architect",
                        "citation_type": "rag_low_confidence",
                        "citation_confidence": 0.2,
                        "confidence_threshold": 0.5,
                    },
                    {"stage": "mel", "citation_type": "rag_support", "citation_confidence": 0.6},
                ],
            },
            "job_events": [
                {
                    "event_id": "qa1",
                    "ts": "2026-02-24T10:00:00+00:00",
                    "type": "status_changed",
                    "to_status": "accepted",
                },
                {
                    "event_id": "qa2",
                    "ts": "2026-02-24T10:00:05+00:00",
                    "type": "status_changed",
                    "to_status": "running",
                },
                {
                    "event_id": "qa3",
                    "ts": "2026-02-24T10:00:20+00:00",
                    "type": "status_changed",
                    "to_status": "pending_hitl",
                },
                {"event_id": "qa4", "ts": "2026-02-24T10:01:20+00:00", "type": "resume_requested"},
                {"event_id": "qa5", "ts": "2026-02-24T10:01:30+00:00", "type": "status_changed", "to_status": "done"},
            ],
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-quality-job-2",
        {
            "status": "done",
            "hitl_enabled": True,
            "generate_preflight": {"warning_level": "low", "risk_level": "low"},
            "state": {
                "donor_id": donor,
                "quality_score": 6.5,
                "critic_score": 6.0,
                "needs_revision": False,
                "logframe_draft": {
                    "indicators": [
                        {
                            "indicator_id": "IND_003",
                            "name": "Adoption of AI governance framework at ministry level",
                            "baseline": "0 ministries",
                            "target": "4 ministries",
                            "frequency": "annual",
                            "formula": "Count of ministries adopting framework by decree",
                            "definition": "Number of ministries with formal AI governance framework adopted.",
                            "data_source": "Government decree records",
                            "disaggregation": ["ministry_type"],
                            "result_level": "impact",
                        }
                    ]
                },
                "critic_notes": {
                    "fatal_flaws": [
                        {
                            "finding_id": "f3",
                            "severity": "medium",
                            "status": "acknowledged",
                            "section": "logframe",
                            "code": "BASELINE_TARGET_MISSING",
                            "message": "Indicators are missing complete baseline and target fields.",
                            "source": "llm",
                            "label": "BASELINE_TARGET_MISSING",
                        }
                    ],
                    "rule_checks": [{"code": "logframe.complete", "status": "pass"}],
                    "llm_advisory_diagnostics": {
                        "advisory_applies": False,
                        "advisory_candidate_count": 1,
                        "advisory_rejected_reason": "grounding_threshold_not_met",
                        "candidate_label_counts": {"BASELINE_TARGET_MISSING": 1},
                    },
                },
                "citations": [
                    {
                        "stage": "architect",
                        "citation_type": "rag_claim_support",
                        "citation_confidence": 0.8,
                        "confidence_threshold": 0.7,
                    }
                ],
            },
            "job_events": [
                {
                    "event_id": "qb1",
                    "ts": "2026-02-24T11:00:00+00:00",
                    "type": "status_changed",
                    "to_status": "accepted",
                },
                {
                    "event_id": "qb2",
                    "ts": "2026-02-24T11:00:03+00:00",
                    "type": "status_changed",
                    "to_status": "running",
                },
                {"event_id": "qb3", "ts": "2026-02-24T11:00:25+00:00", "type": "status_changed", "to_status": "done"},
            ],
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-quality-job-3",
        {
            "status": "error",
            "hitl_enabled": False,
            "generate_preflight": {"warning_level": "high", "risk_level": "high"},
            "state": {"donor_id": "eu", "quality_score": 4.0, "critic_score": 3.0, "needs_revision": True},
            "job_events": [
                {
                    "event_id": "qc1",
                    "ts": "2026-02-24T12:00:00+00:00",
                    "type": "status_changed",
                    "to_status": "accepted",
                },
                {
                    "event_id": "qc2",
                    "ts": "2026-02-24T12:00:02+00:00",
                    "type": "status_changed",
                    "to_status": "running",
                },
                {"event_id": "qc3", "ts": "2026-02-24T12:00:10+00:00", "type": "status_changed", "to_status": "error"},
            ],
        },
    )
    runtime_state.JOB_STORE.set(
        "portfolio-quality-job-4",
        {
            "status": "done",
            "hitl_enabled": True,
            "generate_preflight": {"warning_level": "medium", "risk_level": "medium"},
            "state": {
                "donor_id": donor,
                "quality_score": 6.0,
                "critic_score": 5.5,
                "needs_revision": True,
                "critic_notes": {
                    "fatal_flaws": [
                        {
                            "finding_id": "f4",
                            "severity": "medium",
                            "status": "open",
                            "section": "toc",
                            "code": "GROUNDING_WEAK",
                            "message": "Grounding evidence is mostly fallback.",
                        }
                    ],
                    "rule_checks": [
                        {"code": "toc.grounding", "status": "warn"},
                        {"code": "TOC_TEXT_COMPLETENESS", "status": "fail"},
                    ],
                },
                "citations": [
                    {"stage": "architect", "citation_type": "fallback_namespace", "citation_confidence": 0.2},
                    {"stage": "mel", "citation_type": "fallback_namespace", "citation_confidence": 0.2},
                ],
            },
            "job_events": [
                {"event_id": "qd1", "ts": "2026-02-24T13:00:00+00:00", "type": "status_changed", "to_status": "done"}
            ],
        },
    )
    return donor
