"""Independent batch-text UI overlay, retaining task and local draft identities."""
import argparse
import json
from pathlib import Path
import shutil
import sys

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT))
from pallet.mask_model_training_data import read,write,require
from pallet.cluster_targets import digest
from pallet.pallet_description_review import SETS
from pallet.pallet_description_ui import assets
from pallet.pallet_description_simple import ROOT,POLICY
from pallet.pallet_description_batch import RELEASE
from scripts.build_mask_remaining_direct_requests import build_font

CODE=('pallet/pallet_description_review.py','pallet/pallet_description_ui.py','pallet/pallet_description_simple.py',
    'pallet/pallet_description_batch.py','pallet/assets/pallet_description_batch.js','scripts/serve_pallet_description_common.py',
    'scripts/check_pallet_description_simple.py','scripts/check_pallet_description_batch.py','pallet/pallet_description_single.py',
    'scripts/check_pallet_description_single.py','pallet/pallet_description_attributes.py','scripts/check_pallet_description_attributes.py')


def build(mode,release=RELEASE,attributes=False):
    s=SETS[mode];out=release/mode;require(not out.exists(),'Keep previous batch overlay');out.mkdir(mode=0o700)
    previous=ROOT/mode;original=read(previous/'manifest.json');text=''
    for name,value in assets(mode).items():(out/name).write_text(value);text+=value
    require("request(plan.endpoint+'/apply',body)" in text and "'structure-batch':'structure-attributes-batch'" in text and 'structureAttributesBatch' in text
        and 'descriptionConfirmed' not in text and 'observationState' not in text,'Wrong batch UI')
    shutil.copyfile(previous/'description_drafts.jsonl',out/'description_drafts.jsonl')
    require(digest(out/'description_drafts.jsonl')==original['assets']['description_drafts.jsonl'],'Initial descriptions changed')
    text+=(out/'description_drafts.jsonl').read_text()+json.dumps(read(s['root']/'classes.json'),ensure_ascii=False)
    build_font(out,text)
    from pallet.pallet_description_single import REVIEWER_POLICY
    write(out/'manifest.json',dict(policy=POLICY,mode=mode,reviewer_policy=REVIEWER_POLICY,batch_structure_enabled=True,batch_attributes_enabled=attributes,batch_scope='current_navigation_sequence_text_or_attributes_separate' if attributes else 'current_navigation_sequence_text_only',
        task_manifest_sha256=digest(s['root']/'manifest.json'),proposals_sha256=digest(s['root']/'proposals.jsonl'),
        assets={n:digest(out/n) for n in ('page.html','review.js','review.css','report-font.otf','description_drafts.jsonl')},
        targets=original['targets'],default_normal=original['default_normal'],previous_overlay_manifest_sha256=digest(previous/'manifest.json')))
    print(dict(mode=mode,state='batch_text_overlay_built',targets=original['targets']))


if __name__=='__main__':
    p=argparse.ArgumentParser();p.add_argument('mode',choices=SETS);a=p.parse_args();build(a.mode)
