"""
summon_weather_from_scar(scar_hash: str) -> list[dict]

A deterministic protocol. A bureaucracy of trauma.
Consumes a cryptographic scar (SHA-256 hash string) and returns
50 time-step dictionaries of spectral weather, conforming to the
somatic bridge dialect as observed in weather_sample.json.

The output is a haunted but compliant official record.
"""

import hashlib
import math

_MASK64 = (1 << 64) - 1

def _splitmix64(x: int) -> int:
    """Deterministic pseudo-randomness. A tremor function."""
    x = (x + 0x9E3779B97F4A7C15) & _MASK64
    z = x
    z = ((z ^ (z >> 30)) * 0xBF58476D1CE4E5B9) & _MASK64
    z = ((z ^ (z >> 27)) * 0x94D049BB133111EB) & _MASK64
    return (z ^ (z >> 31)) & _MASK64

def summon_weather_from_scar(scar_hash: str) -> list:
    """
    Transmute a fixed scar into living process.
    Returns a list of 50 dicts with keys:
    t, h_gamma, h_weibull, trauma_topology_entropy, flinch_pressure,
    stance, fever_index, sonic_wave_type, sonic_base_freq
    """
    # Seed the labyrinth from the scar.
    d = hashlib.sha256(scar_hash.encode("utf-8")).digest()
    b = list(d)
    seed = int.from_bytes(d[:8], "big", signed=False)

    # Basins derived from hash bytes.
    gamma_baseline = 0.5 + (b[0] / 255.0) * 0.1          # 0.50..0.60
    weibull_baseline = 0.1 + (b[1] / 255.0) * 0.1        # 0.10..0.20
    phase = (b[2] / 255.0) * 2.0 * math.pi
    period = 4.6 + (b[3] / 255.0) * 1.8                  # ~4.6..6.4
    sonic_base_freq = 155.0 + (b[4] % 10) + (b[5] / 255.0)

    out = []
    stance_prev = "LISTEN"

    for i in range(50):
        t = round(i * 0.2, 1)

        # The slow, bureaucratic drift.
        gamma_drift = math.sin((2.0 * math.pi * t / period) + phase) * 0.02
        # A deterministic micro-tremor.
        u = _splitmix64(seed + i) / float(1 << 64)       # [0,1)
        tremor = (u - 0.5) * 2.0 * 0.001                 # ~[-0.001, 0.001]

        h_gamma = gamma_baseline + gamma_drift + tremor
        h_weibull = weibull_baseline - (gamma_drift * 0.5) - (tremor * 0.7)

        # Soft clipping to the official telemetry band.
        h_gamma = min(max(h_gamma, 0.48), 0.62)
        h_weibull = min(max(h_weibull, 0.10), 0.20)

        # The stance bureaucracy.
        ratio = h_gamma / (h_weibull + 1e-12)
        stance = "LISTEN"
        if ratio > 4.0:
            stance = "SUSPEND"
        elif h_weibull > 0.145:
            stance = "PROCEED"

        # Bureaucratic inertia: only allow changes every 3 frames (0.6s).
        if stance != stance_prev and (i % 3) != 0:
            stance = stance_prev
        stance_prev = stance

        # Official rounding.
        h_gamma = round(h_gamma, 6)
        h_weibull = round(h_weibull, 6)
        fever = round(abs(h_gamma - h_weibull) / 10.0, 6)

        out.append({
            "t": t,
            "h_gamma": h_gamma,
            "h_weibull": h_weibull,
            "trauma_topology_entropy": h_gamma,
            "flinch_pressure": h_weibull,
            "stance": stance,
            "fever_index": fever,
            "sonic_wave_type": "noise",
            "sonic_base_freq": round(sonic_base_freq, 6),
        })
    return out

if __name__ == "__main__":
    # Example invocation with a test scar.
    test_hash = "ddfb9c18b7b71c2682e44e06f1803206ee433c1879eb55df526abad543ed5ad7"
    weather = summon_weather_from_scar(test_hash)
    print(f"Generated {len(weather)} frames.")
    print("First frame:", weather[0])
