"""Append generated/reused descriptions to osszesito_leirasok.xlsx.
Reads _new_batch.json: list of {cks:[...], short, long}  (name/cat looked up from _new_products.json).
One row appended per cikkszám: [Cikkszám, Termék Név, Kategória, Rövid, Hosszú, Rövid kar., Hosszú kar.].
Char counts include HTML (matches existing rows). Atomic save + progress update.
Usage: python append_osszesito.py [-f]   (-f skips the 800-1050 char check, e.g. for reuse)
"""
import openpyxl, json, os, sys, io, re, time
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

BASE = os.path.dirname(os.path.abspath(__file__))
DST = os.path.join(BASE, 'osszesito_leirasok.xlsx')
PROGRESS = os.path.join(BASE, '_new_progress.json')
BATCH = os.path.join(BASE, '_new_batch.json')
FORCE = '-f' in sys.argv

# name/cat lookup
meta = {d['ck']: (d['name'], d['cat']) for d in
        json.load(open(os.path.join(BASE, '_new_products.json'), encoding='utf-8'))}

with open(BATCH, encoding='utf-8') as f:
    text = f.read()
# autofix: Hungarian opening quote followed by ASCII closing quote
text_fixed = re.sub(r'„([^"]{1,80})"', lambda m: '„' + m.group(1) + '”', text)
if text_fixed != text:
    with open(BATCH, 'w', encoding='utf-8') as f:
        f.write(text_fixed)
    print('Auto-fixed Hungarian quotes')
batch = json.loads(text_fixed)

# validation
warn = False
for item in batch:
    sl = len(item['short']); ll = len(item['long'])
    tag = item['cks'][0]
    if not FORCE and (sl < 750 or sl > 1050):
        print(f'WARN {tag} short={sl}'); warn = True
    if not FORCE and (ll < 750 or ll > 1050):
        print(f'WARN {tag} long={ll}'); warn = True
    for ck in item['cks']:
        if ck not in meta:
            print(f'ERROR: {ck} not in _new_products.json'); warn = True
if warn and not FORCE:
    print('ABORT: char-count/meta violation. Fix _new_batch.json or rerun with -f.')
    sys.exit(1)

# load progress (dedupe against already-written)
prog = json.load(open(PROGRESS, encoding='utf-8'))
done = set(prog.get('done_cks', []))

wb = openpyxl.load_workbook(DST)
ws = wb['Leírások']
appended = 0
for item in batch:
    short = item['short']; lng = item['long']
    for ck in item['cks']:
        if ck in done:
            continue
        name, cat = meta[ck]
        ws.append([ck, name, cat, short, lng, len(short), len(lng)])
        done.add(ck); appended += 1

tmp = DST + '.tmp'
wb.save(tmp); wb.close()
for i in range(30):
    try:
        os.replace(tmp, DST); break
    except PermissionError:
        if i == 0: print('File locked (OneDrive sync?), retrying...')
        time.sleep(2)
else:
    raise PermissionError(f'Could not replace {DST} after 60s')

prog['done_cks'] = list(done)
tmp = PROGRESS + '.tmp'
json.dump(prog, open(tmp, 'w', encoding='utf-8'), ensure_ascii=False, indent=1)
os.replace(tmp, PROGRESS)

print(f'OK: appended {appended} rows. Total done: {len(done)} / 1003')
