"""Tests for scripts/kb_proposal_normalize.py."""

from __future__ import annotations

import sys
from pathlib import Path

REPO_ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO_ROOT / "scripts"))

import kb_proposal_normalize as norm  # noqa: E402


def test_freeform_claim_split_is_blocked_until_canonical_ids_exist():
    preview = norm.normalize_proposal(
        {
            "id": "p-freeform",
            "proposal_type": "attach_evidence",
            "status": "approved",
            "payload": {
                "old_claim": {"id": "d0000000-0000-0000-0000-000000000005"},
                "claim_candidates": [
                    {
                        "claim_key": "new_claim_key",
                        "headline": "New claim headline",
                        "edges": [
                            {
                                "edge_type": "supersedes_component_of",
                                "target_claim_id": "d0000000-0000-0000-0000-000000000005",
                            }
                        ],
                        "evidence": [{"source_key": "human_review"}],
                    }
                ],
                "source_candidates": [{"source_key": "human_review"}],
            },
        }
    )

    assert preview["normalization_state"] == "blocked_missing_canonical_ids"
    assert preview["strict_child_count"] == 0
    assert preview["blocked_count"] == 2
    assert preview["blocked_fragments"][0]["missing"] == ["canonical from_claim"]
    assert preview["blocked_fragments"][1]["missing"] == ["canonical claim_id", "canonical source_id"]
    assert "Create or identify canonical claim/source rows" in preview["next_normalization_action"]


def test_exact_edge_fragment_emits_strict_add_edge_child():
    preview = norm.normalize_proposal(
        {
            "id": "p-edge",
            "proposal_type": "attach_evidence",
            "status": "approved",
            "payload": {
                "proposed_supersession_edges": [
                    {
                        "from_claim": "11111111-1111-1111-1111-111111111111",
                        "to_claim_id": "22222222-2222-2222-2222-222222222222",
                        "edge_type": "supports",
                        "weight": 0.7,
                    }
                ]
            },
        }
    )

    assert preview["normalization_state"] == "strict_children_ready"
    assert preview["blocked_count"] == 0
    assert preview["strict_child_count"] == 1
    child = preview["strict_child_proposals"][0]
    assert child["proposal_type"] == "add_edge"
    assert child["payload"]["apply_payload"] == {
        "from_claim": "11111111-1111-1111-1111-111111111111",
        "to_claim": "22222222-2222-2222-2222-222222222222",
        "edge_type": "supports",
        "weight": 0.7,
    }


def test_exact_evidence_fragments_emit_one_attach_evidence_child():
    preview = norm.normalize_proposal(
        {
            "id": "p-evidence",
            "proposal_type": "attach_evidence",
            "status": "approved",
            "payload": {
                "evidence": [
                    {
                        "claim_id": "33333333-3333-3333-3333-333333333333",
                        "source_id": "44444444-4444-4444-4444-444444444444",
                        "role": "grounds",
                        "weight": 0.9,
                    }
                ]
            },
        }
    )

    assert preview["normalization_state"] == "strict_children_ready"
    assert preview["strict_child_count"] == 1
    child = preview["strict_child_proposals"][0]
    assert child["proposal_type"] == "attach_evidence"
    assert child["payload"]["apply_payload"]["evidence"] == [
        {
            "claim_id": "33333333-3333-3333-3333-333333333333",
            "source_id": "44444444-4444-4444-4444-444444444444",
            "role": "grounds",
            "weight": 0.9,
        }
    ]


def test_existing_apply_payload_is_already_strict():
    preview = norm.normalize_proposal(
        {
            "id": "p-strict",
            "proposal_type": "add_edge",
            "status": "approved",
            "payload": {
                "apply_payload": {
                    "from_claim": "11111111-1111-1111-1111-111111111111",
                    "to_claim": "22222222-2222-2222-2222-222222222222",
                    "edge_type": "supports",
                }
            },
        }
    )

    assert preview["normalization_state"] == "already_strict_apply_payload"
    assert preview["strict_child_count"] == 0
    assert preview["blocked_count"] == 0
