from __future__ import annotations
import json, os
from pathlib import Path
ROOT=Path('/home/agent/.hermes/private/finance/sprint16')
OUT=Path('/home/agent/jarvis_runtime/finance-system/sprint16.1-uat')
mappings=json.loads((OUT/'source-mappings-private.json').read_text())
by={}
for m in mappings: by.setdefault(m['source_type'],[]).append(m['mapping_id'])
def text(name):
 data=(ROOT/name).read_bytes()
 for enc in ('utf-8-sig','cp1252'):
  try:return data.decode(enc)
  except UnicodeDecodeError:pass
 raise RuntimeError('unsupported encoding')
files=[
 {'profile':'raiffeisen_bank','mapping_ids':sorted(by['raiffeisen_bank']),'csv_text':text('raiffeisen.csv')},
 {'profile':'akb_bank','mapping_id':by['akb_bank'][0],'csv_text':text('akb_transactions.csv')},
 {'profile':'visa_credit_card','mapping_ids':sorted(by['visa_credit_card']),'source_reference':'visa-card','csv_text':text('visa.csv')},
 {'profile':'migros_receipts','csv_text':text('migros.csv')},
]
payload={'files':files}
p=OUT/'real-preview-payload-private.json'
fd=os.open(p,os.O_CREAT|os.O_EXCL|os.O_WRONLY,0o600)
with os.fdopen(fd,'w') as f:json.dump(payload,f,ensure_ascii=False,separators=(',',':'))
print(json.dumps({'payload':str(p),'mode':oct(p.stat().st_mode&0o777),'profiles':[x['profile'] for x in files],'file_bytes':[len(x['csv_text'].encode()) for x in files],'total_bytes':sum(len(x['csv_text'].encode()) for x in files)},indent=2))
