"""Operator-facing rollback coverage posture for shipped autonomy write paths."""

from __future__ import annotations

from collections import Counter
from typing import Any


_ROLLBACK_COVERAGE_ITEMS: tuple[dict[str, str], ...] = (
    {
        "write_class_id": "sequence_pause",
        "label": "Sequence pause",
        "module_id": "sequence_pause",
        "action_key": "sequence_pause",
        "tool_name": "sequence.pause",
        "posture": "exercised",
        "reason": "Failure drills restore CRM pause tags and resume paused Outreach sequence states.",
    },
    {
        "write_class_id": "offboarding_reassign_owner",
        "label": "Offboarding owner reassignment",
        "module_id": "offboarding_cleanup",
        "action_key": "offboarding_cleanup",
        "tool_name": "user.offboard -> sfdc.record.reassign_owner",
        "posture": "exercised",
        "reason": "Failure drills restore original Salesforce owner assignments.",
    },
    {
        "write_class_id": "offboarding_deactivate_user",
        "label": "Offboarding user deactivation",
        "module_id": "offboarding_cleanup",
        "action_key": "offboarding_cleanup",
        "tool_name": "user.offboard -> sfdc.user.deactivate",
        "posture": "manual_compensation_required",
        "reason": "Reactivating users can reintroduce licenses, access, and downstream automation side effects.",
    },
    {
        "write_class_id": "deal_desk_update_fields",
        "label": "Deal desk update fields",
        "module_id": "deal_desk",
        "action_key": "deal_desk_update_fields",
        "tool_name": "sfdc.record.update",
        "posture": "exercised",
        "reason": "Snapshot rollback restores the original Salesforce field values.",
    },
    {
        "write_class_id": "deal_desk_approval_stamp",
        "label": "Deal desk approval stamp",
        "module_id": "deal_desk",
        "action_key": "deal_desk_approval_stamp",
        "tool_name": "dealdesk.approval.stamp",
        "posture": "exercised",
        "reason": "Failure drills restore the stamped approval fields from snapshot rollback data.",
    },
    {
        "write_class_id": "auto_enrich_missing_fields",
        "label": "Auto-enrich missing fields",
        "module_id": "auto_enrich_missing_fields",
        "action_key": "auto_enrich_missing_fields",
        "tool_name": "sfdc.record.enrich_missing_fields",
        "posture": "manual_compensation_required",
        "reason": "Clearing enriched fields can break downstream automation and data dependencies.",
    },
    {
        "write_class_id": "follow_up_enforcement",
        "label": "Follow-up enforcement",
        "module_id": "follow_up_enforcement",
        "action_key": "follow_up_enforcement",
        "tool_name": "sfdc.followup.enforce",
        "posture": "manual_compensation_required",
        "reason": "Deleting created tasks or restoring stages can disrupt user workflows and ownership queues.",
    },
    {
        "write_class_id": "deal_desk_create_renewal",
        "label": "Deal desk create renewal shell",
        "module_id": "deal_desk",
        "action_key": "deal_desk_create_renewal",
        "tool_name": "dealdesk.renewal.create_shell",
        "posture": "manual_compensation_required",
        "reason": "Deleting created renewal shells is destructive and can orphan follow-on activity.",
    },
    {
        "write_class_id": "slack_routing",
        "label": "Slack routing",
        "module_id": "slack_routing",
        "action_key": "slack_routing",
        "tool_name": "notification.slack -> slack.notify",
        "posture": "intentionally_irreversible",
        "reason": "Slack notifications are not retractable once delivered.",
    },
)


def rollback_coverage_snapshot() -> dict[str, Any]:
    items = [dict(entry) for entry in _ROLLBACK_COVERAGE_ITEMS]
    counts = Counter(str(entry.get("posture") or "") for entry in items)
    summary = {
        "exercised": int(counts.get("exercised", 0)),
        "manual_compensation_required": int(counts.get("manual_compensation_required", 0)),
        "intentionally_irreversible": int(counts.get("intentionally_irreversible", 0)),
        "total": len(items),
    }
    return {
        "summary": summary,
        "items": items,
    }


__all__ = ["rollback_coverage_snapshot"]
