#!/usr/bin/env python3
from pathlib import Path
import json,hashlib,datetime,subprocess,sys,os
R=Path('/data/video-pipeline/HaTramAudio/project/027-Nguoi-Thua-Ke-Khong-Nhan-Chiec-Ghe-Danh-San');W=R/'work/artwork';OUT=R/'image/provider';GEN=Path.home()/'.hermes/skills/content-creation/tao-anh/scripts/generate_image.py';CANON='4db101987ecb4d1447cb9c898eba5785db54d614f0c32b16a6b7bdb6cbfbd306';RUN='run-20260722T063859Z-aa8eca81'
prompts=[('intro_poster','Cinematic Vietnamese audio-drama title poster, rainy Suzhou canal street beside a restored wood workshop, a poised Vietnamese woman in a cream coat holding an old brass key and a document folder, an adult Vietnamese woman worker in a dark blue work jacket behind her, one empty wooden chair in the composition, restrained teal and warm amber palette, cinematic minimal composition, room for elegant typography. Add exactly these visible text blocks and no other text: "Hạ Trâm Audio"; "Người Thừa Kế Không Nhận Chiếc Ghế Dành Sẵn"; "Truyện được phát độc quyền tại Hạ Trâm Audio, nghiêm cấm sao chép dưới mọi hình thức". No period after the final word, no extra words, no watermark, no collage, no slide.'),('left_panel','Cinematic minimal audio branding panel, rainy canal window at night, tabletop studio microphone, headphones, warm tea lamp and wooden desk, no people, elegant dark teal and amber palette, upright portrait composition, full bleed. Add exactly two visible text blocks and no other text: "Hạ Trâm Audio" and "Like • Chia sẻ • Đăng ký". No title, no plot text, no watermark, no collage, no slide.'),('right_panel','Cinematic minimal upright Vietnamese romance audio-drama portrait, the same woman from the poster holding an antique brass key and document folder in a rainy traditional canal street, a wooden chair visible behind her, adult woman worker blurred in the distance, teal gray and warm amber palette, full bleed. Add exactly these visible text blocks and no other text: "Hạ Trâm Audio"; "Người Thừa Kế Không Nhận Chiếc Ghế Dành Sẵn"; "Truyện được phát độc quyền tại Hạ Trâm Audio, nghiêm cấm sao chép dưới mọi hình thức". No period after the final word, no extra words, no watermark, no collage, no slide.')]
for name,prompt in prompts:
 out=OUT/f'{name}-provider.png';rec={'schema_version':1,'project_id':'027','run_id':RUN,'asset':name,'source_canon_sha256':CANON,'prompt_sha256':hashlib.sha256(prompt.encode()).hexdigest(),'status':'creating','provider_skill':'tao-anh','created_at':datetime.datetime.now(datetime.timezone.utc).isoformat()};(W/f'{name}.intent.json').write_text(json.dumps(rec,ensure_ascii=False,indent=2)+'\n')
 pp=W/f'{name}.prompt.txt';pp.write_text(prompt);r=subprocess.run([sys.executable,str(GEN),'--prompt',prompt,'--output',str(out),'--n','1','--size','auto','--quality','auto','--background','auto','--detail','high','--format','png','--timeout','600'],stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True,timeout=720)
 if r.returncode or not out.exists(): rec.update({'status':'failed','error':(r.stderr or r.stdout)[-1000:]});(W/f'{name}.receipt.json').write_text(json.dumps(rec,ensure_ascii=False,indent=2)+'\n');raise SystemExit(f'artwork failed: {name}')
 data=out.read_bytes();rec.update({'status':'completed','verified':True,'path':str(out.relative_to(R)),'sha256':hashlib.sha256(data).hexdigest(),'bytes':len(data),'completed_at':datetime.datetime.now(datetime.timezone.utc).isoformat()});(W/f'{name}.receipt.json').write_text(json.dumps(rec,ensure_ascii=False,indent=2)+'\n')
print(json.dumps({'status':'provider_artwork_completed','assets':3},ensure_ascii=False))
