#!/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('v',Path(__file__).with_name('validate_shanyin_screenwriting_integration.py'))
v=importlib.util.module_from_spec(spec); spec.loader.exec_module(v)
base=json.loads((ROOT/'references'/'shanyin-integration-test-fixture.json').read_text(encoding='utf-8'))

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

def ctl(d): d['controller']='shanyin'
def ir(d): d['creates_new_ir']=True
def budget(d): d['load_plan']['active_auxiliaries'].extend(['SHORT_DRAMA_MEDIA_DELTA','NOVEL_GENRE_AUX'])
def no_story_first(d): d['shanyin_aux']['story_first_applied']=False
def template_first(d): d['structural_examination']['template_generated_story']=True
def abs_paren(d): d['shanyin_aux']['absolute_parenthetical_ban']=True
def percentile(d): d['shanyin_aux']['percentile_claim']='TOP_5_PERCENT'
def word_rule(d): d['shanyin_aux']['words_per_minute_status']='HARD_RULE'
def forms(d): d['shanyin_aux']['form_primary_count']=2
def no_fit(d): d['shanyin_aux']['form_fit_gate_passed']=False
def imitate(d): d['shanyin_aux']['examples_used_as_mechanism_only']=False
def platform(d): d['shanyin_aux']['platform_specific_calls_used']=True
def fast_leak(d): d['shanyin_aux']['fast_recipe_mode']='ON'
def no_end(d): d['treatment']['ending_defined']=False
def no_aftertaste(d): d['treatment']['audience_aftertaste_defined']=False
def no_absence(d): d['structural_examination']['intentional_absence_allowed']=False
def overclaim(d): d['release_claim']='HUMAN_REVIEWED'
def adaptation(d):
 d['request']['entry_mode']='FROM_SOURCE'
 d['shanyin_aux']['story_first_applied']=False

def explicit_fast(d):
 d['request']['explicit_fast_recipe']=True
 d['shanyin_aux']['fast_recipe_mode']='ON'


def wrong_primary_capability(d): d['load_plan']['primary_capability']='CREATIVE_TO_NARRATIVE'
def wrong_runtime_role(d): d['shanyin_aux']['runtime_role']='AUXILIARY'
def wrong_primary_stages(d): d['shanyin_aux']['primary_stages']=['W2']
def no_retire(d): d['shanyin_aux']['retires_at']='W7'

def bad_aggression(d): d['shanyin_aux']['aggression_level']='A9_MAXIMUM'
def weak_full_delegation(d): d['shanyin_aux']['aggression_level']='A1_CHALLENGE'
def aggression_claim(d): d['shanyin_aux']['aggression_is_quality_claim']=True
def force_darkness(d): d['shanyin_aux']['darkness_required']=True
def bypass_creative(d): d['shanyin_aux']['route_break_requires_creative_decision']=False
def incomplete_attack(d): d['shanyin_aux']['attack_dossier']['survivor_core']=''
def storyboard_owner(d): d['shanyin_aux']['full_storyboard_owned_by_shanyin']=True
def no_handoff(d): d['shanyin_aux']['narrative_form_to_director_handoff_required']=False
def bad_compiler(d): d['shanyin_aux']['director_compiler']='SHANYIN'
def incomplete_handoff(d): d['shanyin_aux']['director_handoff']['forbidden_literalization']=''

cases=[
 run('positive'),
 run('reject_second_controller',ctl,'runtime controller changed'),
 run('reject_new_ir',ir,'created new IR'),
 run('enforce_aux_budget',budget,'budget exceeded'),
 run('require_story_first_for_seed',no_story_first,'requires story-first'),
 run('reject_template_story_engine',template_first,'template generated story'),
 run('reject_absolute_parenthetical_ban',abs_paren,'ban reintroduced'),
 run('reject_percentile_claim',percentile,'percentile'),
 run('keep_word_rate_heuristic',word_rule,'words-per-minute'),
 run('limit_primary_form',forms,'too many narrative forms'),
 run('require_form_fit',no_fit,'form fit'),
 run('reject_example_imitation',imitate,'imitation'),
 run('reject_platform_call_leak',platform,'platform-specific'),
 run('reject_implicit_fast_recipe',fast_leak,'without consent'),
 run('require_defined_ending',no_end,'treatment incomplete'),
 run('require_aftertaste',no_aftertaste,'treatment incomplete'),
 run('allow_intentional_structure_absence',no_absence,'absence forbidden'),
 run('reject_release_overclaim',overclaim,'exceeds evidence'),
 run('allow_source_route_without_free_story_first',adaptation),
 run('allow_explicit_fast_recipe',explicit_fast),
 run('require_shanyin_primary_capability',wrong_primary_capability,'primary capability'),
 run('require_shanyin_runtime_role',wrong_runtime_role,'lead role'),
 run('require_shanyin_primary_stages',wrong_primary_stages,'primary stages'),
 run('require_shanyin_retirement_gate',no_retire,'retirement gate'),
 run('reject_invalid_aggression_level',bad_aggression,'invalid aggression level'),
 run('require_A2_for_full_delegation_original',weak_full_delegation,'requires A2 default'),
 run('reject_aggression_as_quality_claim',aggression_claim,'quality claim'),
 run('reject_forced_darkness',force_darkness,'darkness forced'),
 run('require_creative_for_route_break',bypass_creative,'bypassed Creative'),
 run('require_complete_attack_dossier',incomplete_attack,'attack dossier incomplete'),
 run('reject_shanyin_storyboard_ownership',storyboard_owner,'storyboard ownership'),
 run('require_director_handoff',no_handoff,'director handoff missing'),
 run('require_valid_director_compiler',bad_compiler,'invalid director compiler'),
 run('require_complete_director_handoff',incomplete_handoff,'director handoff incomplete'),
]
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)
