"""Deterministic adapter model; no Matter stack, radio, or real lamp involved."""
from dataclasses import dataclass
import json


@dataclass
class Gateway:
    session: str = 'boot-A'
    sequence: int = -1
    reported: bool | None = None
    desired: bool | None = None
    pending: bool = False
    timed_out: bool = False

    def command(self, value: bool):
        self.desired, self.pending, self.timed_out = value, True, False

    def report(self, session: str, sequence: int, value: bool):
        if session != self.session or sequence <= self.sequence:
            return False
        self.sequence, self.reported = sequence, value
        # Matching observed state is not proof that our command caused it.
        if self.pending and value == self.desired:
            self.pending = False
        return True

    def timeout(self):
        if self.pending:
            self.pending, self.timed_out = False, True

    def establish_session(self, session: str):
        # Caller must establish/authenticate the session; packets cannot switch it.
        self.session, self.sequence, self.reported = session, -1, None
        self.desired, self.pending, self.timed_out = None, False, False


def run():
    cases = []
    g = Gateway(); g.report('boot-A', 10, False); g.command(True)
    naive_display = True  # optimistic assignment upon queue acceptance
    assert naive_display != g.reported and g.pending
    cases.append({'case': 'accepted_but_not_reported', 'naive_display': naive_display,
                  'reported': g.reported, 'pending': g.pending})
    g.timeout()
    assert g.reported is False and g.timed_out
    cases.append({'case': 'timeout', 'reported': g.reported, 'outcome': 'unknown'})
    assert g.report('boot-A', 11, True)
    assert not g.report('boot-A', 11, True)
    assert not g.report('boot-A', 10, False)
    assert g.reported is True
    cases.append({'case': 'late_duplicate_and_reordered_reports', 'reported': g.reported})
    g.command(False)
    g.report('boot-A', 12, True)
    assert g.pending and g.reported is True
    g.report('boot-A', 13, False)
    assert not g.pending
    cases.append({'case': 'conflicting_then_matching_report', 'reported': g.reported})
    g.establish_session('boot-B')
    assert not g.report('boot-A', 99, True)
    assert g.report('boot-B', 0, False)
    cases.append({'case': 'device_reboot', 'session': g.session, 'sequence': g.sequence})
    # Without a sequence/session in the transport, identical bytes cannot prove freshness.
    last = False
    readings = [True, True]  # first true then cached replay, or two distinct physical events
    accepted = []
    for value in readings:
        if value != last:
            accepted.append(value); last = value
    assert len(accepted) == 1
    cases.append({'case': 'payload_only_dedupe', 'reports_seen': 2, 'accepted': 1,
                  'cannot_distinguish': 'replay vs fresh same-value observation'})
    return cases


if __name__ == '__main__':
    print(json.dumps({'model': 'adapter-state-v1', 'passed': 6, 'cases': run()}, indent=2))
