#!/usr/bin/env python3
from __future__ import annotations
import copy, importlib.util, json
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
spec=importlib.util.spec_from_file_location('sd',Path(__file__).with_name('validate_short_drama_delta.py'))
mod=importlib.util.module_from_spec(spec); spec.loader.exec_module(mod)
base=json.loads((ROOT/'references'/'short-drama-delta-test-fixture.json').read_text(encoding='utf-8'))

def run(name,mut=None,expected=None):
    d=copy.deepcopy(base)
    if mut: mut(d)
    e=mod.validate(d)
    ok=(not e) if expected is None else any(expected in x for x in e)
    return {'name':name,'status':'PASS' if ok else 'FAIL','errors':e,'expected':expected}

def fixed(d): d['hard_fixed_scene_count']=True
def twist(d): d['twist_required']=True
def hook_only(d): d['episode_local_payoff_present']=False
def no_series(d): d['series_arc_delta_present']=False
def no_private(d): d['private_stake_visible_before_climax']=False
def bad_controller(d): d['controller']='short-drama-master'
def too_many(d): d['active_auxiliaries_count']=3
def fake_claim(d): d['release_claim']='BETTER_THAN_EXTERNAL'

cases=[
 run('positive_short_drama_delta'),
 run('reject_fixed_scene_count',fixed,'fixed scene count is forbidden'),
 run('reject_forced_twist',twist,'forced twist is forbidden'),
 run('reject_hook_only_episode',hook_only,'hook-only episode is forbidden'),
 run('reject_no_series_delta',no_series,'episode must change the series state'),
 run('reject_private_stake_after_climax',no_private,'private stake must be visible before climax'),
 run('reject_second_controller',bad_controller,'controller must remain juben'),
 run('reject_auxiliary_overload',too_many,'active auxiliary budget exceeded'),
 run('reject_fake_comparison_claim',fake_claim,'release claim exceeds evidence'),
]
status='PASS' if all(x['status']=='PASS' for x in cases) else 'FAIL'
print(json.dumps({'status':status,'passed':sum(x['status']=='PASS' for x in cases),'total':len(cases),'results':cases},ensure_ascii=False,indent=2))
raise SystemExit(0 if status=='PASS' else 1)
