#!/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_jinghua_final.py'))
mod=importlib.util.module_from_spec(spec); spec.loader.exec_module(mod)
base=json.loads((ROOT/'references'/'jinghua-test-fixture.json').read_text(encoding='utf-8'))

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

def f(path,value):
    def m(d):
        cur=d
        for k in path[:-1]: cur=cur[k]
        cur[path[-1]]=value
    return m
cases=[
 run('positive'),
 run('controller',f(['controller'],'jinghua'),'controller must remain juben'),
 run('truth_owner',f(['story_truth_owner'],'roundtable'),'Creative must retain story truth'),
 run('new_ir',f(['creates_new_ir'],True),'must not create a new IR'),
 run('relation_classes',lambda d:d['relational_morality'].update({'toward_strangers':'','toward_enemies':''}),'at least three'),
 run('moral_activation',f(['relational_morality','activation_conditions'],[]),'activation conditions'),
 run('cheap_redeem',f(['selective_humanity','does_not_erase_harm'],False),'may not erase harm'),
 run('no_cost_humanity',f(['selective_humanity','actual_loss_accepted'],False),'accept cost'),
 run('contradiction_no_pressure',f(['contradiction','pressure_event'],''),'contradiction missing'),
 run('reveal_no_past',f(['revelation','past_event_reinterpreted'],''),'reinterpret past'),
 run('reveal_no_future',f(['revelation','future_action_forced'],''),'force future action'),
 run('unfair_reveal',f(['revelation','fair_clues'],False),'fair clues'),
 run('trauma_no_strategy',f(['trauma_strategy','survival_strategy'],''),'trauma-strategy'),
 run('ending_no_cost',f(['ending','cost'],''),'ending missing'),
 run('reversible_climax',f(['ending','choice_irreversible'],False),'irreversible'),
 run('debt_ignored',f(['ending','causal_debt_accounted'],False),'causal debt'),
 run('scale_no_permission',f(['scale_permission','permission'],''),'scale-permission'),
 run('no_later_horizon',f(['three_horizon','later'],''),'three horizon'),
 run('opponent_no_fallback',f(['opponent_horizon','fallback'],''),'opponent horizon'),
 run('too_many_peaks',f(['intensity_budget','primary_peaks'],['a','b','c']),'exceed two'),
 run('no_lowered_axis',f(['intensity_budget','deliberately_lowered_axes'],[]),'lower other axes'),
 run('theme_by_speech',f(['theme','requires_summary_line'],True),'summary line'),
 run('aftermath_reset',f(['aftermath','persists_next_scene'],False),'aftermath must persist'),
 run('tone_contamination',f(['genre_tone','all_genres_forced_dark'],True),'forced dark'),
 run('network_isolated',f(['network_consequence','secondary_actors_reactivated'],1),'at least two'),
 run('overclaim',f(['release_claim'],'HUMAN_REVIEWED'),'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)
