"""Runnable offline application-journal example. Python 3.9+, standard library.
No service request/response contract, network access or cryptographic signing.
Synthetic states below are local application states, not provider enums.
"""
from dataclasses import dataclass, field
import json

@dataclass
class Journal:
    case_ref: str
    document_ref: str
    local_state: str = 'prepared'
    service_ref: str = ''
    applied_receipts: set = field(default_factory=set)
    reconciliation_required: bool = False
    final_document_observed: bool = False
    validation_observed: bool = False

    def mark_submitted(self):
        if self.local_state != 'prepared':
            raise ValueError('Reconcile before creating another request')
        self.local_state = 'submitted'

    def response_lost(self):
        self.local_state = 'unknown_outcome'
        self.reconciliation_required = True

    def reconcile(self, service_ref, result):
        if not service_ref:
            raise ValueError('Obtain a service correlation via the agreed lookup/support process')
        self.service_ref = service_ref
        self.local_state = result
        self.reconciliation_required = False

    def receive(self, receipt, service_ref, result):
        # receipt is a local durable receipt key, not an invented provider event ID.
        if receipt in self.applied_receipts:
            return 'duplicate_ignored'
        if not self.service_ref or self.service_ref != service_ref:
            self.reconciliation_required = True
            return 'correlation_requires_review'
        self.applied_receipts.add(receipt)
        if self.local_state == 'signature_observed' and result != 'signature_observed':
            self.reconciliation_required = True
            return 'out_of_order_requires_review'
        self.local_state = result
        return 'applied'

    def accept_final_document(self):
        if self.local_state != 'signature_observed':
            raise ValueError('A final document must not be assumed before a signing result')
        self.final_document_observed = True

    def record_validation(self):
        if not self.final_document_observed:
            raise ValueError('Read a separate validation result for the final document')
        self.validation_observed = True


def main():
    j = Journal('case_demo_001', 'doc_demo_001')
    j.mark_submitted()
    j.response_lost()
    try:
        j.mark_submitted()
    except ValueError:
        print('Unknown outcome: automatic resubmission blocked')
    # Synthetic lookup fixture, not a real provider response or status enum.
    j.reconcile('service_demo_001', 'signature_observed')
    assert j.receive('receipt_demo_001', 'service_demo_001', 'signature_observed') == 'applied'
    assert j.receive('receipt_demo_001', 'service_demo_001', 'signature_observed') == 'duplicate_ignored'
    assert j.receive('receipt_demo_002', 'service_demo_001', 'awaiting_user') == 'out_of_order_requires_review'
    assert j.local_state == 'signature_observed'
    assert not j.final_document_observed and not j.validation_observed
    j.reconcile('service_demo_001', 'signature_observed')
    j.accept_final_document()
    j.record_validation()
    print(json.dumps({'simulation': True, 'wire_response': False, 'network_calls': 0,
                      'case_ref': j.case_ref, 'document_ref': j.document_ref,
                      'service_ref': j.service_ref, 'final_document_observed': j.final_document_observed,
                      'validation_observed': j.validation_observed}, indent=2))

if __name__ == '__main__':
    main()
