"""Local teaching fixture. No network, credentials, or production effects.
Python 3.10+. The fake destination is single-process and has no crash durability.
Its atomic key/effect behavior is an assumption, not an implementation recipe.
"""
class Destination:
    def __init__(self):
        self.operations = {}
        self.effects = 0

    def apply(self, key, parameters, lose_response=False):
        if key in self.operations:
            old_parameters, receipt = self.operations[key]
            if old_parameters != parameters:
                raise ValueError("same key, different intent")
            return receipt
        self.effects += 1
        receipt = f"resource-{self.effects}"
        self.operations[key] = (dict(parameters), receipt)
        if lose_response:
            raise TimeoutError("response lost after effect")
        return receipt


def reconcile(status, matches_intent):
    if status == "applied" and matches_intent:
        return "verify_postcondition"
    return "hold_for_owner"


def main():
    parameters = {"target": "test-pool", "size": 1}
    destination = Destination()
    try:
        destination.apply("operation-a", parameters, lose_response=True)
    except TimeoutError:
        pass
    assert destination.effects == 1
    receipt = destination.apply("operation-a", parameters)
    assert receipt == "resource-1" and destination.effects == 1
    try:
        destination.apply("operation-a", {"target": "other-pool", "size": 1})
    except ValueError:
        pass
    else:
        raise AssertionError("changed intent was accepted")
    assert destination.effects == 1
    assert reconcile("applied", True) == "verify_postcondition"
    for status, matches in [("not_found", True), ("unavailable", True), ("applied", False)]:
        assert reconcile(status, matches) == "hold_for_owner"
    naive = Destination()
    try:
        naive.apply("first-key", parameters, lose_response=True)
    except TimeoutError:
        naive.apply("new-key", parameters)
    assert naive.effects == 2
    print("PASS: replay, changed intent, reconciliation holds, and naive duplicate demonstrated")


if __name__ == "__main__":
    main()
