"""
RGD-PHYSICS :: THERMODYNAMICS LAW
Enforces the Landauer Principle: Information processing generates heat.
"""
from ..core.interfaces import PhysicsLaw, Microsecond, PhysicsReport
from ..chronon.interfaces import ChrononProtocol
from ..core.exceptions import ThermodynamicLimit

class Thermodynamics(PhysicsLaw):
    """
    Audits the entropy generated by state changes.
    """
    
    def __init__(self, max_dissipation_rate: float = 100.0):
        # Joules per second (Watts) equivalent
        self._max_cooling = max_dissipation_rate

    @property
    def name(self) -> str:
        return "thermodynamics"

    def audit(self, t0: ChrononProtocol, t1: ChrononProtocol, dt: Microsecond) -> PhysicsReport:
        """
        Checks if the cognitive effort generates too much entropy.
        """
        # 1. ESTIMATE ENTROPY DELTA (dS)
        # We check the change in Payload Hash. 
        # A large change implies massive information rewriting.
        # (Heuristic: simple difference in integer value of the hash normalized)
        # In a real system, this would be a Hamming distance or compression delta.
        hash_diff = abs(t1.payload_hash - t0.payload_hash)
        delta_s = float(hash_diff % 1024) # Normalized entropy unit
        
        # 2. CALCULATE HEAT GENERATION (dQ/dt)
        # Power = Work / Time
        if dt <= 0: return {"status": "INSTANTANEOUS_SKIP"}
        heat_rate = delta_s / dt
        
        # 3. CHECK THERMAL THROTTLING
        if heat_rate > self._max_cooling:
            raise ThermodynamicLimit(
                f"THERMAL CRITICAL: Heat Rate {heat_rate:.2f} > Cooling Capacity {self._max_cooling}. "
                "System needs to throttle down."
            )
            
        return {
            "entropy_delta": delta_s,
            "heat_rate": heat_rate,
            "status": "NOMINAL"
        }