#!/usr/bin/env python3
from __future__ import annotations
import re,sys,json
from pathlib import Path
GLOBAL=['【来源剧本】','【来源字数用途】','【媒介】','【画幅】','【目标成片时长】','【格式Profile】']
SCENE=['【来源节拍】','【节拍聚类】','【观察权】','【信息释放】','【场景密度Profile】','【剪辑能量曲线】','【镜头数量】','【理想剪辑轨】','【安全覆盖轨】','【机位设置】','【制作展开账本】']
SHOT=['来源','轨道','拍摄时长','计划成片使用','机位组','合法展开类型','剪辑职责','主功能','戏剧任务','观察理由','信息增量','景别/角度/机位高度','构图与焦点','轴线/屏幕方向/视线','镜头运动','调度与画面动作','表演与反应','对白','声音','时长预算','剪辑接口','状态入','状态出','连续性风险','制作风险与降级']
BAD=['**配音台词**','**背景音效**','字=','字＝','每3秒切','每4秒切']
def validate_text(t):
 e=[];w=[]
 for x in GLOBAL:
  if x not in t:e.append('missing global '+x)
 for x in SCENE:
  if x not in t:e.append('missing scene decision '+x)
 blocks=re.split(r'(?=^### [A-Za-z0-9_-]+$)',t,flags=re.M);ids=[]
 for b in blocks:
  m=re.match(r'^### ([A-Za-z0-9_-]+)$',b)
  if not m:continue
  q=m.group(1);ids.append(q)
  for f in SHOT:
   if not re.search(rf'- \*\*{re.escape(f)}\*\*：',b):e.append(f'{q}: missing {f}')
  if '**轨道**：SAFETY_COVERAGE' in b and not re.search(r'\*\*计划成片使用\*\*：0(?:\.0)?s',b):e.append(f'{q}: safety coverage counted in edit')
 if len(ids)!=len(set(ids)):e.append('duplicate shot ids')
 for x in BAD:
  if x in t:e.append('forbidden legacy or count-ratio text '+x)
 if 'N_MIN' not in t or 'N_COVERAGE' not in t or 'N_EDIT' not in t or 'N_SETUP' not in t:e.append('missing four shot counts')
 return e,w
if __name__=='__main__':
 p=Path(sys.argv[1] if len(sys.argv)>1 else 'fixtures/rendered-storyboard-test-fixture.md');e,w=validate_text(p.read_text(encoding='utf-8'));print(json.dumps({'status':'PASS' if not e else 'FAIL','errors':e,'warnings':w},ensure_ascii=False,indent=2));raise SystemExit(0 if not e else 1)
