"""
Receipt factories for mass-based routing law-change episode.

Activation of mass escalation requires this 5-receipt chain:
1. InvariantViolationReceipt - documents the problem
2. SelfRepairProposalReceipt - proposes the fix (kind=ROUTING_CHANGE)
3. SandboxRunReceipt - evidence from canary testing
4. CouncilDecisionReceipt - human approval
5. SelfRepairOutcomeReceipt - post-activation metrics
"""

from __future__ import annotations

from typing import Dict, List


def create_mass_escalation_violation_receipt(
    evidence_episode_ids: List[str],
    description: str = "Foundational anchors routed to low-tier providers without escalation",
) -> Dict:
    """Receipt 1: Document why mass escalation is needed."""
    return {
        "receipt_type": "invariant_violation",
        "violation_type": "routing_underprotection",
        "description": description,
        "evidence_episode_ids": evidence_episode_ids,
        "severity": "medium",
        "proposed_remedy": "Enable mass-based tier escalation",
    }


def create_mass_escalation_proposal_receipt(
    violation_receipt_id: str,
    thresholds: Dict = None,
) -> Dict:
    """Receipt 2: Propose enabling mass escalation."""
    return {
        "receipt_type": "self_repair_proposal",
        "proposal_kind": "ROUTING_CHANGE",
        "title": "Enable mass-based tier escalation",
        "description": "High-mass context (m_eff > threshold) escalates routing tier",
        "payload": {
            "feature_flag": "enable_mass_escalation",
            "escalation_thresholds": thresholds
            or {"tier_4": 0.8, "tier_3": 0.6, "tier_2": 0.3},
            "mass_version": "sft.mass.v0.1",
        },
        "parent_ids": [violation_receipt_id],
    }


def create_mass_escalation_sandbox_receipt(
    proposal_receipt_id: str,
    episodes_tested: int,
    escalations_triggered: int,
    dignity_violations: int,
    latency_p99_ms: float,
) -> Dict:
    """Receipt 3: Evidence from sandbox/canary testing."""
    return {
        "receipt_type": "sandbox_run",
        "proposal_id": proposal_receipt_id,
        "episodes_tested": episodes_tested,
        "results": {
            "escalations_triggered": escalations_triggered,
            "dignity_violations": dignity_violations,
            "latency_impact_p99_ms": latency_p99_ms,
            "anchor_protection_improved": escalations_triggered > 0 and dignity_violations == 0,
        },
        "recommendation": "PROMOTE" if dignity_violations == 0 else "REJECT",
    }


def create_mass_escalation_council_receipt(
    proposal_receipt_id: str,
    decision: str,
    approver: str,
    rationale: str,
    scope: str = "canary_10_percent",
) -> Dict:
    """Receipt 4: Human/council approval decision."""
    return {
        "receipt_type": "council_decision",
        "proposal_id": proposal_receipt_id,
        "decision": decision,
        "approver": approver,
        "rationale": rationale,
        "effective_scope": scope,
    }


def create_mass_escalation_outcome_receipt(
    proposal_receipt_id: str,
    observation_hours: int,
    incidents_before: int,
    incidents_after: int,
    escalation_rate: float,
    false_escalation_rate: float,
) -> Dict:
    """Receipt 5: Post-activation outcome metrics."""
    return {
        "receipt_type": "self_repair_outcome",
        "proposal_id": proposal_receipt_id,
        "observation_window_hours": observation_hours,
        "metrics": {
            "anchor_corruption_incidents_before": incidents_before,
            "anchor_corruption_incidents_after": incidents_after,
            "escalation_rate": escalation_rate,
            "false_escalation_rate": false_escalation_rate,
        },
        "status": "SUCCESS" if incidents_after < incidents_before else "MONITORING",
    }
