#!/usr/bin/env python3
from pathlib import Path
import json,subprocess,sys
ROOT=Path('/data/video-pipeline/HaTramAudio/project/019-Buc-Tuong-Giua-Hai-Ban-Cong');RUN='run-20260721T065616Z-a7dbad2f';OWNER='Levy';LEASE='/home/hermes/.hermes/skills/audio-production/ha-tram-audio/scripts/gate_lease.py';COMMIT='/home/hermes/.hermes/skills/audio-production/ha-tram-audio/scripts/commit_gate.py'
def run(args,**kw):subprocess.check_call(args,cwd=ROOT,**kw)
def main():
 m=json.loads((ROOT/'script/project-manifest.json').read_text());e=m.get('gate_events',{}).get('story',{})
 if m.get('steps',{}).get('story')!='completed' or e.get('verified') is not True:raise RuntimeError('Story Gate not committed')
 for gate in ['tts','metadata','poster','layout']:
  run(['python3',LEASE,'acquire','--project-root',str(ROOT),'--gate',gate,'--run-id',RUN,'--owner',OWNER,'--ttl-seconds','7200'],stdout=subprocess.DEVNULL)
 run([sys.executable,'work/build_metadata.py'])
 run(['python3',COMMIT,'--project-root',str(ROOT),'--gate','metadata','--receipt',str(ROOT/'log/metadata.json'),'--run-id',RUN])
 run([sys.executable,f'work/artwork/{RUN}/build_prompts.py'])
 print(json.dumps({'status':'ready','metadata_committed':True,'tts_command':[sys.executable,'work/tts/run_tts.py'],'artwork_command':[sys.executable,f'work/artwork/{RUN}/generate_artwork.py']},ensure_ascii=False))
if __name__=='__main__':main()
