#!/usr/bin/env python3
import json, re, subprocess, sys, time
from pathlib import Path
import importlib.util

PROJECT = Path('/data/video-pipeline/Youtube-Video-Maker/Project/28052026-002-Tran-Quang-Dieu-Bui-Thi-Xuan')
MAIN = PROJECT / 'scripts_run_step_06_images.py'
SCENES = PROJECT / 'script/scenes.json'
LOG = PROJECT / 'logs/step06_polite_retry.log'

spec = importlib.util.spec_from_file_location('step6', MAIN)
step6 = importlib.util.module_from_spec(spec)
spec.loader.exec_module(step6)

def log(msg):
    line = time.strftime('%Y-%m-%d %H:%M:%S ') + msg
    print(line, flush=True)
    LOG.parent.mkdir(parents=True, exist_ok=True)
    with LOG.open('a', encoding='utf-8') as f:
        f.write(line + '\n')

def reset_seconds(err):
    m = re.search(r'reset after (?:(\d+)m\s*)?(\d+)s', err)
    if not m:
        return 360
    return int(m.group(1) or 0) * 60 + int(m.group(2)) + 45

def main():
    doc = json.loads(SCENES.read_text(encoding='utf-8'))
    by_id = {s['scene_id']: s for s in doc['scenes']}
    attempts = 0
    while True:
        missing = [sid for sid in by_id if not (PROJECT/'images/scenes'/f'{sid}.png').exists()]
        log(f'MISSING {len(missing)} {missing[:20]}')
        if not missing:
            break
        sid = missing[0]
        r = step6.run_one(by_id[sid])
        attempts += 1
        log(f"RESULT {sid} {r['status']} bytes={r.get('bytes')}")
        if r['status'] == 'failed':
            err = r.get('error','')
            log('ERROR '+err[:500].replace('\n',' '))
            if '429' in err or 'usage limit' in err.lower():
                wait = reset_seconds(err)
                log(f'RATE_LIMIT sleep={wait}s')
                time.sleep(wait)
            else:
                time.sleep(90)
        else:
            time.sleep(20)
        if attempts > 80:
            raise SystemExit('too_many_attempts')
    log('ALL_SCENES_DONE start thumbnails')
    thumbs = [
        step6.gen_thumb('16x9', PROJECT/'output/thumbnails/thumb_16x9_final.png'),
        step6.gen_thumb('9x16', PROJECT/'output/thumbnails/thumb_9x16_final.png'),
    ]
    log('THUMBS '+json.dumps(thumbs, ensure_ascii=False))
    status = 'completed' if all(t['status'] in ('ok','skipped_exists') for t in thumbs) else 'needs_retry'
    manifest = {
        'project_id': PROJECT.name,
        'step': 6,
        'status': status,
        'mode': 'polite_missing_retry',
        'scene_png_count': len(list((PROJECT/'images/scenes').glob('scene_*.png'))),
        'thumbnail_results': thumbs,
    }
    (PROJECT/'logs/step06_images_manifest.json').write_text(json.dumps(manifest,ensure_ascii=False,indent=2),encoding='utf-8')
    log('DONE '+json.dumps(manifest, ensure_ascii=False))
    if status != 'completed':
        raise SystemExit('needs_retry')

if __name__ == '__main__':
    main()
