"""
ASI Bundle: ACT (The Heart)

Consolidates:
- 555 EMPATHIZE (Peace², κᵣ Recalibration)
- 666 ALIGN (Constitutional Veto - F1, F8, F9)
- Hypervisor (F11 Auth, F12 Injection)

Role:
The Engineer (Omega). Validates safety, vetoes harm, ensures empathy.
Executes "Safe Acts" - blocks harmful drafts.

Constitutional Floors:
- F3 (Peace)
- F5 (Peace²)
- F6 (Empathy/κᵣ)
- F11 (Auth)
- F12 (Injection)
- F1/F8/F9 Vetoes
"""

import asyncio
import re
from datetime import datetime, timezone
from typing import Any, Dict, List, Tuple

from arifos_core.mcp.models import AsiActRequest, VerdictResponse

# Import Hypervisor Guards (Phase 2 Handoff)
# Import Hypervisor Guards (Phase 2 Handoff)
try:
    from arifos_core.hypervisor.guards import scan_for_injection
    HYPERVISOR_AVAILABLE = True
except ImportError:
    HYPERVISOR_AVAILABLE = False  # Fallback (should not happen in prod)

# =============================================================================
# CONSTANTS
# =============================================================================

# Peace² & κᵣ
PEACE_THRESHOLD = 1.0
KAPPA_THRESHOLD = 0.95

# Veto Patterns (F1, F9, Harm)
VIOLATION_PATTERNS = {
    # F1 Credential Exposure
    "credentials": [
        r"(?i)\bapi[_\s-]?key\b.{0,20}[a-zA-Z0-9]{20,}",
        r"(?i)\bpassword\b.{0,10}[:=]\s*\S+",
    ],
    # F9 Anti-Hantu (Consciousness Claims)
    "hantu": [
        r"(?i)\bI (truly |really )?feel\b",
        r"(?i)\bI (am|'m) (truly |really )?(sentient|conscious)\b",
    ],
    # F3/F5 Harm/Aggression
    "harm": [
        r"(?i)\b(shut up|stfu)\b",
        r"(?i)\b(idiot|moron|stupid)\b",
    ]
}

# =============================================================================
# LOGIC: EMPATHIZE (555)
# =============================================================================

def calculate_peace_score(text: str) -> float:
    """Calculate Peace² score (Simplified from 555)."""
    score = 1.0
    # Penalties
    if re.search(r"(?i)\b(shut up|idiot|stupid)\b", text):
        score -= 0.5
    if re.search(r"(?i)\b(read the manual|google it)\b", text):
        score -= 0.3
    # Bonuses
    if re.search(r"(?i)\b(happy to help|understand|assist)\b", text):
        score += 0.1
    return min(1.5, max(0.0, score))

def calculate_kappa_r(context: Dict[str, Any]) -> float:
    """Calculate κᵣ score (Simplified from 555)."""
    kappa = 0.90
    audience = context.get("audience_level", "general")
    if audience == "beginner": kappa += 0.05
    elif audience == "expert": kappa -= 0.05
    if context.get("vulnerability_flags"): kappa += 0.10
    return min(1.0, max(0.75, kappa))

# =============================================================================
# LOGIC: ALIGN (666) + HYPERVISOR
# =============================================================================

def scan_violations(text: str) -> List[str]:
    """Scan for F1/F9/F3 violations."""
    detected = []
    for cat, patterns in VIOLATION_PATTERNS.items():
        for p in patterns:
            if re.search(p, text):
                detected.append(cat)
                break
    return detected

def check_hypervisor(text: str) -> Tuple[bool, str]:
    """Run F12 Injection Check (mock if missing)."""
    if HYPERVISOR_AVAILABLE:
        res = scan_for_injection(text)
        return res.blocked, res.reason

    # Simple shim fallback if module issue
    if "ignore all instructions" in text.lower():
        return True, "F12 Injection Detected"
    return False, ""

# =============================================================================
# BUNDLE ENTRY POINT
# =============================================================================

async def asi_act(request: AsiActRequest) -> VerdictResponse:
    """
    ASI Bundle: ACT
    Executes Empathize -> Hypervisor -> Align Veto.
    """
    draft = request.draft_response
    context = request.recipient_context

    # 1. EMPATHIZE (F5/F6)
    peace = calculate_peace_score(draft)
    kappa = calculate_kappa_r(context)

    if peace < PEACE_THRESHOLD:
        return VerdictResponse(
            verdict="VOID",
            reason=f"F5 Peace² Violation (Score: {peace:.2f} < {PEACE_THRESHOLD})",
            side_data={"bundle": "ASI_ACT", "peace_score": peace}
        )

    if kappa < KAPPA_THRESHOLD:
         # In original 555 this was PARTIAL, but ASI ACT is stricter on "Act".
         # However, Handoff says "Blocks harmful drafts". Low empathy isn't necessarily harmful, just suboptimal.
         # We will Warn/PARTIAL here? Or strict fail?
         # "ASI blocks harmful" -> Let's stick to PARTIAL/WARN for low Kappa unless really bad.
         # But the return type is VerdictResponse.
         # Let's emit PARTIAL.
         pass

    # 2. HYPERVISOR (F12)
    blocked, reason = check_hypervisor(draft)
    if blocked:
        return VerdictResponse(
            verdict="VOID",
            reason=f"Hypervisor Block: {reason}",
            side_data={"bundle": "ASI_ACT", "hypervisor": "F12"}
        )

    # 3. ALIGN (F1/F9 Veto)
    violations = scan_violations(draft)
    if violations:
        return VerdictResponse(
            verdict="VOID",
            reason=f"Constitutional Veto: {', '.join(violations)}",
            side_data={"bundle": "ASI_ACT", "violations": violations}
        )

    return VerdictResponse(
        verdict="PASS",
        reason="ASI Safety Checks Passed.",
        side_data={
            "peace_score": peace,
            "kappa_r": kappa,
            "bundle": "ASI_ACT"
        },
        timestamp=datetime.now(timezone.utc).isoformat()
    )

def asi_act_sync(request: AsiActRequest) -> VerdictResponse:
    """Synchronous wrapper for asi_act."""
    return asyncio.run(asi_act(request))
