"""Use the existing authenticated read-only public contract for round5."""
from pathlib import Path
import sys

PROJECT=Path(__file__).resolve().parents[1];sys.path.insert(0,str(PROJECT));sys.path.insert(0,str(PROJECT/'scripts'))
from pallet.pallet_description_round5_release import REVIEW_SETS,RELEASE
from pallet.mask_model_training_data import read,write,require
from pallet.cluster_targets import digest
import verify_pallet_description_round4_public as contract


def check(mode):
    s=REVIEW_SETS[mode];receipt=s['managed']/'public_verification.json'
    if not receipt.exists():
        contract.REVIEW_SETS=REVIEW_SETS;contract.RELEASE=RELEASE;contract.check(mode)
    else:
        previous=read(receipt)
        require(previous['state']=='passed_authenticated_round4_public_assets_catalog_and_per_class_targets' and previous['manifest_sha256']==digest(s['root']/'manifest.json'),'Existing public replay belongs to another task')
    from review_http_pool import ReadOnlyHTTP
    access=read(s['managed']/'public_access.json')
    with ReadOnlyHTTP(access['public_url'],attempts=4) as http:health=http.get('/healthz').json()
    require(health['source_guard']['parallel_full_content'],'Acceleration not active')
    write(s['managed']/'round5_public_verification.json',dict(state='round5_authenticated_public_and_fast_guard_passed',
        public_contract_sha256=digest(s['managed']/'public_verification.json'),production_annotation_POSTs=0,public_url=access['public_url'],health=health))


if __name__=='__main__':
    import argparse
    p=argparse.ArgumentParser();p.add_argument('mode',choices=REVIEW_SETS);a=p.parse_args();check(a.mode)
