from __future__ import annotations
import copy
from app.main import _scenario_seed_docs

scenario={
 'id':'scenario-test','title':'Scenario Test','version':3,'summary':'A rich test','canon_mode':'Strict Canon','parent_character':'Xaden',
 'payload':{
   'scenario_schema':'melumiyachat_scenario_dossier_v3','premise':'A dangerous academy scenario.',
   'scenario_setup':'The rebellion is hidden from most cadets.',
   'history_backstory':['The rebellion ended five years ago.'],
   'relationship_setup':['Xaden and Mara begin as rivals.'],
   'hard_scenario_rules':['Dragons choose their riders.'],
   'locations':['Basgiath War College'],
   'secrets_knowledge':[{'fact':'Brennan is alive','known_by':['Xaden'],'hidden_from':['Mara'],'stable_key':'secret:brennan'}],
   'groups_factions':['Fourth Wing'],
   'important_objects':['Mara carries a silver key.'],
   'world_rules':['Signets manifest after bonding.'],
   'current_state':['Mara has just entered the Riders Quadrant.'],
   'physical_continuity':['Xaden has a scar over one eyebrow.'],
   'story_timeline':['Conscription Day begins.'],
   'goals_hooks':['Survive the parapet.'],
   'key_characters':['Violet Sorrengail — rider'],
   'starting_scene':{'location':'Parapet','date_or_era':'Basgiath first year','time':'Morning','present_cast':['Xaden','Mara'],'situation':'Mara is about to cross.'},
   'parent_character_id':'char-xaden','parent_character_snapshot':{'id':'char-xaden','title':'Xaden','payload':{'identity':{'full_name':'Xaden'}}},
   'linked_characters':[{'content_id':'char-violet','name':'Violet Sorrengail','role':'rider'}],
   'web_research':{'sources':[{'url':'https://example.test'}]},
 }
}
original=copy.deepcopy(scenario)
docs,_,_,_=_scenario_seed_docs(scenario,{'persona_name':'Mara','start_date':'2026-08-19','start_time':'09:00'})
w=docs['world_bible']
assert w['schema_version'] >= 4
assert any('rebellion ended' in str(x).lower() for x in w['history_backstory'])
assert any('dragons choose' in str(x).lower() for x in w['core_canon'])
assert any('basgiath' in str(x).lower() for x in w['locations'])
assert any('fourth wing' in str(x).lower() for x in w['groups_factions'])
assert any('silver key' in str(x).lower() for x in w['important_objects'])
assert any('signets manifest' in str(x).lower() for x in w['world_rules'])
assert any('entered the riders' in str(x).lower() for x in w['current_state'])
assert any('scar' in str(x).lower() for x in w['physical_continuity'])
assert any('conscription' in str(x).lower() for x in w['story_timeline'])
assert any('survive the parapet' in str(x).lower() for x in w['custom_entries'])
assert docs['knowledge']['facts'][0]['known_by']==['Xaden']
assert docs['knowledge']['facts'][0]['hidden_from']==['Mara']
assert docs['scene']['location']=='Parapet'
assert docs['scene']['present']==['Xaden','Mara']
assert docs['hard_canon']['scenario_schema']=='melumiyachat_scenario_dossier_v3'
assert scenario==original, 'Scenario template was mutated while seeding RP'
w['locations'][0]['details']='CHANGED ONLY IN RP'
assert scenario==original, 'RP world still aliases scenario payload'
print('PASS — structured Scenario template clones cleanly into RP state')
