"""Merge AI descriptions into original Rövid Leírás column.

Strategy B: Where the AI columns are filled, overwrite the original
short description (column 10 = 'Rövid Leírás') with AI Short (column 104).
The AI Long (column 105) is kept and renamed to 'Hosszú Leírás'.

Usage:
    python merge_descriptions.py              # Strategy 1 (default): short → col 10, long stays at 105 renamed
    python merge_descriptions.py --concat     # Strategy 2: col 10 = short + <br><br> + long (single field)

Input:  merged_master_v2_updated.xlsx
Output: merged_master_v2_FINAL.xlsx
"""
import openpyxl, os, sys, io, argparse
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

BASE = os.path.dirname(os.path.abspath(__file__))
SRC = os.path.join(BASE, 'merged_master_v2_updated.xlsx')
DST = os.path.join(BASE, 'merged_master_v2_FINAL.xlsx')

ROVID_COL = 11        # 'Rövid Leírás' (1-indexed openpyxl)
AI_SHORT_COL = 104    # 'Rövid Leírás (AI)'
AI_LONG_COL = 105     # 'Hosszú Leírás (AI)'

def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--concat', action='store_true', help='Concat short + long into col 10 (Strategy 2)')
    args = ap.parse_args()

    print(f'Loading {SRC}...')
    wb = openpyxl.load_workbook(SRC)
    ws = wb['Sheet1']

    headers = [c.value for c in next(ws.iter_rows(min_row=1, max_row=1))]
    print(f'Found {ws.max_row-1} rows.')

    # Sanity check
    if headers[ROVID_COL-1] != 'Rövid Leírás':
        print(f'WARN: col {ROVID_COL} is "{headers[ROVID_COL-1]}", expected "Rövid Leírás"')
    if headers[AI_SHORT_COL-1] != 'Rövid Leírás (AI)':
        print(f'WARN: col {AI_SHORT_COL} is "{headers[AI_SHORT_COL-1]}", expected "Rövid Leírás (AI)"')
    if headers[AI_LONG_COL-1] != 'Hosszú Leírás (AI)':
        print(f'WARN: col {AI_LONG_COL} is "{headers[AI_LONG_COL-1]}", expected "Hosszú Leírás (AI)"')

    overwritten = 0
    long_kept = 0

    for r in range(2, ws.max_row + 1):
        ai_short = ws.cell(row=r, column=AI_SHORT_COL).value
        ai_long = ws.cell(row=r, column=AI_LONG_COL).value

        if ai_short and str(ai_short).strip():
            if args.concat and ai_long and str(ai_long).strip():
                # Strategy 2: combined
                ws.cell(row=r, column=ROVID_COL, value=str(ai_short).strip() + '<br><br>' + str(ai_long).strip())
            else:
                # Strategy 1: only short to col 10
                ws.cell(row=r, column=ROVID_COL, value=str(ai_short).strip())
            overwritten += 1

        if ai_long and str(ai_long).strip():
            long_kept += 1

    # Rename headers for clarity
    if not args.concat:
        # Rename col 105 'Hosszú Leírás (AI)' → 'Hosszú Leírás' so it's a clean export column
        ws.cell(row=1, column=AI_LONG_COL, value='Hosszú Leírás')
        # Optionally clear col 104 header (since data is now in col 11)
        ws.cell(row=1, column=AI_SHORT_COL, value='Rövid Leírás (AI - backup)')

    print(f'Overwritten Rövid Leírás (col 11): {overwritten} rows')
    print(f'AI Long present:                   {long_kept} rows')
    print(f'Mode: {"concat (short+long in col 11)" if args.concat else "short only in col 11, long in col 105"}')

    print(f'Saving {DST}...')
    wb.save(DST)
    wb.close()
    print('Done.')

if __name__ == '__main__':
    main()
