"""Synthetic settlement branches applying C-DEEL-DEPOSIT; not an instruction to offset."""
from decimal import Decimal
from pathlib import Path
import json,sys
def settle(deposit,invoice,opening,offset_confirmed=False,refund_credit_date=None):
    deposit=Decimal(str(deposit));invoice=Decimal(str(invoice));balance=Decimal(str(opening))
    offset=invoice if offset_confirmed and deposit>invoice else Decimal(0)
    held=deposit-offset;external=invoice-offset
    balance-=external
    events=[dict(date='2027-01-29',kind='final invoice',external_cash_gbp=str(external),deposit_offset_gbp=str(offset),available_gbp=str(balance),held_gbp=str(held))]
    minimum=balance
    if refund_credit_date:
        balance+=held;events.append(dict(date=refund_credit_date,kind='conditional confirmed refund credit',external_cash_gbp=str(-held),deposit_offset_gbp='0',available_gbp=str(balance),held_gbp='0'));held=Decimal(0)
    return dict(events=events,minimum_balance_gbp=str(minimum),first_cash_shortfall='2027-01-29' if minimum<0 else None,held_at_horizon_gbp=str(held),refund_date_status='explicit hypothetical credit date' if refund_credit_date else 'unknown: no spendable refund included')
if __name__=='__main__':
    rows={'larger_deposit_confirmed_offset':settle(8000,7500,8000,True),'smaller_deposit_no_offset_refund_unknown':settle(8000,9000,8000,True),'smaller_deposit_conditional_credit':settle(8000,9000,8000,True,'2027-03-30'),'equal_deposit_no_assumed_offset':settle(8000,8000,8000,True)}
    Path(sys.argv[1]).write_text(json.dumps({'label':'Synthetic GBP settlement cases, not customer outcomes or guaranteed refund dates','binding':['C-DEEL-DEPOSIT','C-DEEL-REFUND'],'cases':rows},indent=2)+'\n')
