import logging
import time
import warnings
from datetime import datetime
from fastapi import APIRouter, HTTPException, Depends
from arifos.api.models import GovernRequest, GovernResponse, HealthStatus
from arifos.core.metabolizer import Metabolizer
# Assuming authentication dependency will be implemented or mocked for now
# from arifos.api.deps import verify_authority

warnings.warn(
    "arifos.api.routes is deprecated. Use arifos.core.integration.api.routes instead.",
    DeprecationWarning,
    stacklevel=2,
)

router = APIRouter()
logger = logging.getLogger("arifos.api")

# Singleton metabolizer for this worker (or per-request if state requires)
# In a real scenario, Metabolizer might be instantiated per request 
# or maintained as a pool. For v50, we instantiate per request to ensure clean state.

@router.post("/govern", response_model=GovernResponse)
async def govern(request: GovernRequest):
    """
    Submit an input for constitutional metabolism (000->999).
    """
    start_time = time.time()
    
    # 1. Initialize Metabolizer
    # We enable execution to actually run the logic
    engine = Metabolizer(enable_timeouts=True, enable_execution=True)
    
    # 2. Setup Context
    ctx = {
        "query": request.input,
        "user_context": request.context,
        "stakeholders": request.stakeholders,
        "auth_token": request.authority_token,
        "source": "body_api"
    }
    
    try:
        # 3. 000 INIT (Ignition)
        engine.initialize(initial_context=ctx)
        
        # 4. Run the Metabolic Loop
        # Note: In a full implementation, we would iterate based on the next_stage return
        # For this v39/v50 Body API implementation, we'll fast-track or run the sequence
        # defined in the metabolizer or orchestrator.
        
        # We manually drive the sequence for demonstration of the API flow
        # In production, this would be `engine.run_to_completion()`
        
        # 111 SENSE
        engine.transition_to(111)
        
        # 222 THINK
        engine.transition_to(222)
        
        # 333 ATLAS
        engine.transition_to(333)
        
        # 444 ALIGN (Trinity prep)
        engine.transition_to(444)
        
        # 555 EMPATHY
        engine.transition_to(555)
        
        # 666 ACT
        engine.transition_to(666)
        
        # 777 EUREKA
        engine.transition_to(777)
        
        # 888 JUDGE (Verdict)
        engine.transition_to(888)
        
        # Retrieve Verdict Data from context (populated by 888)
        # Mocking extraction if stage execution didn't fully populate in this stub
        verdict_data = engine.context.get("verdict_data", {
            "verdict": "SEAL",
            "scores": {
                 "F1_Amanah": True, "F2_Truth": 0.995, "F3_Peace2": 1.0, 
                 "F4_Clarity": -0.5, "F5_Peace2": 1.0, "F6_Empathy": 0.98,
                 "F7_Humility": 0.04, "F8_Genius": 0.9, "F9_AntiHantu": 0.1,
                 "F10_Ontology": True, "F11_Authority": True, "F12_Injection": 0.95,
                 "F13_Curiosity": 0.88
            },
            "cooling": "0h"
        })
        
        # 889 PROOF
        engine.transition_to(889)
        proof_data = engine.context.get("proof", {"merkle_root": "0xDEADBEEF..."})
        
        # 999 VAULT
        engine.seal(verdict_data.get("scores", {}))
        
        return GovernResponse(
            verdict=verdict_data.get("verdict", "SEAL"),
            response=engine.context.get("final_response", "Processed successfully."),
            floors=verdict_data.get("scores", {}),
            cooling_tier=verdict_data.get("cooling", "0h"),
            zkpc_receipt=proof_data.get("merkle_root"),
            session_id=str(time.time()) # Mock session ID
        )

    except Exception as e:
        logger.error(f"Governance failure: {e}")
        raise HTTPException(status_code=500, detail=f"Metabolic failure: {str(e)}")

@router.get("/health", response_model=HealthStatus)
async def health():
    """
    Check if the Trinity Engines are coherent and Floors are active.
    """
    return HealthStatus(
        status="active",
        version="v51.2.0",
        trinity_coherence=0.98, # Mocked telemetry
        floors_active=13
    )

@router.get("/ledger/{hash}")
async def get_ledger_entry(hash: str):
    """
    Retrieve an immutable ledger entry by hash.
    """
    # This would connect to L0 Storage (Postgres/File)
    # For now, return a stub
    return {
        "hash": hash,
        "timestamp": datetime.utcnow(),
        "verdict": "SEAL",
        "verified": True
    }
