#!/usr/bin/env python3
"""Validate Slice 19B in a fresh SBCL 2.5.4 process with network access denied."""
import argparse
import datetime
import hashlib
import json
import os
import pathlib
import re
import shutil
import subprocess
import tempfile

ROOT = pathlib.Path(__file__).resolve().parent.parent
BASE = '9ce2a2b5116edfd921a5240efcb2f604f9523693'
POLICY = '(version 1)(allow default)(deny network*)'
NAMES = ['validation', 'semantics', 'merging', 'associations', 'cxtm', 'occurrences',
         'prefixes', 'integers', 'decimals', 'dates', 'datetimes', 'timezones',
         'typed-names', 'scopes', 'variants', 'references', 'identifiers', 'reification', 'arity',
         'relationships', 'templates', 'map-import', 'mergemap']


def sha(path):
    return hashlib.sha256(path.read_bytes()).hexdigest()


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--sbcl', default=os.environ.get('SBCL', 'sbcl'))
    parser.add_argument('--output', default=str(ROOT / 'reports/slice-19b'))
    args = parser.parse_args()
    runtime = shutil.which(args.sbcl)
    assert runtime is not None, 'SBCL executable not found'
    output = pathlib.Path(args.output).resolve()
    output.mkdir(parents=True, exist_ok=True)
    protected = {str(p.relative_to(ROOT)): sha(p) for directory in ['vendor/cxtm', 'references']
                 for p in (ROOT / directory).rglob('*') if p.is_file()}
    sources = {str(p.relative_to(ROOT)): sha(p) for directory in ['src', 'tests', 'scripts', 'docs']
               for p in (ROOT / directory).rglob('*') if p.is_file() and
               '__pycache__' not in p.parts and p.suffix in ['.lisp', '.py', '.sh', '.md']}
    sources.update({p: sha(ROOT / p) for p in ['topic-maps.asd', 'README.md']})
    started = datetime.datetime.now(datetime.timezone.utc).isoformat()
    version = subprocess.check_output([runtime, '--version'], text=True).strip()
    assert version == 'SBCL 2.5.4', version
    with tempfile.TemporaryDirectory(prefix='tmdm-slice-19b-validation-') as temp:
        report = pathlib.Path(temp) / 'validation.json'
        command = ['/usr/bin/sandbox-exec', '-p', POLICY, '/usr/bin/env', 'PYTHONDONTWRITEBYTECODE=1',
                   'SBCL=' + runtime, 'TOPIC_MAPS_REPORT=' + str(report), str(ROOT / 'scripts/test.sh')]
        result = subprocess.run(command, cwd=ROOT, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        (output / 'tests.log').write_bytes(result.stdout)
        if result.returncode:
            raise RuntimeError('Full regression failed: see ' + str(output / 'tests.log'))
        log = result.stdout.decode('utf-8')
        for name in NAMES:
            source = report if name == 'validation' else pathlib.Path(str(report) + '.' + name + '.json')
            (output / (name + '.json')).write_bytes(source.read_bytes())
    old = ROOT / 'reports/slice-19a'
    previous = json.loads((old / 'validation.json').read_text())
    now = json.loads((output / 'validation.json').read_text())
    assert now['statuses'] == {'pass': 194, 'fail': 2, 'unsupported': 0, 'not-run': 77}
    selected = sorted('ctm/in/' + n + '.ctm' for n in ['mergemap', 'mergemap-syntax-qname', 'mergemap-template', 'mergemap-tm-reifier'])
    selected += sorted('ctm/invalid/' + n + '.ctm' for n in ['mergemap-missing-syntax-iri', 'mergemap-template', 'mergemap-unknown-syntax'])
    changes = [(a['id'], a['status'], b['status']) for a, b in zip(previous['cases'], now['cases']) if a['status'] != b['status']]
    assert changes == [(id, 'not-run', 'pass') for id in selected]
    assert [c['id'] for c in previous['cases']] == [c['id'] for c in now['cases']]
    for a, b in zip(previous['cases'], now['cases']):
        if a['id'] not in selected:
            assert a == b, a['id']
    for name in NAMES[1:-1]:
        prior = json.loads((old / (name + '.json')).read_text())
        current = json.loads((output / (name + '.json')).read_text())
        if name == 'cxtm':
            assert prior['cases'] == current['cases'][:len(prior['cases'])]
            assert current['contractCounts'] == {'pass': 169, 'fail': 2}
            actual = output / 'actual'
            actual.mkdir(exist_ok=True)
            for case in current['cases'][len(prior['cases']):]:
                baseline = ROOT / 'vendor/cxtm/ctm/baseline' / (pathlib.Path(case['id']).name + '.cxtm')
                raw = case['actualCXTM'].encode('utf-8')
                assert raw == baseline.read_bytes()
                (actual / baseline.name).write_bytes(raw)
        elif name == 'templates':
            # One former UNSUPPORTED-feature assertion becomes the specific
            # unknown-notation diagnostic. No old original contract is dropped.
            old_check = 'diagnostic:unchanged-excluded-feature:%mergemap <other> <urn:ctm>'
            new_check = 'diagnostic:unknown-mergemap-notation'
            assert old_check in prior['checks'] and new_check in current['checks']
            assert set(prior['checks']) - {old_check} == set(current['checks']) - {new_check}
            for key in prior.keys() - {'checks'}:
                assert prior[key] == current[key], name + ':' + key
        else:
            assert prior == current, name
    example = (ROOT / 'docs/reading-ctm-mergemap.md').read_text().split('```lisp\n', 1)[1].split('\n```', 1)[0]
    inspection = subprocess.run(['/usr/bin/sandbox-exec', '-p', POLICY, runtime, '--noinform', '--no-sysinit',
                                 '--no-userinit', '--script', '/dev/stdin'], input=example.encode(), cwd=ROOT,
                                stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    (output / 'describe.log').write_bytes(inspection.stdout)
    assert inspection.returncode == 0, 'Documented inspection forms failed'
    control = """(require :asdf)
(asdf:initialize-source-registry '(:source-registry :ignore-inherited-configuration))
(asdf:load-asd (truename "topic-maps.asd"))
(asdf:load-system "topic-maps/ctm")
(load (make-string-input-stream (uiop:run-program
 (list "git" "show" "BASE:src/ctm/lexer.lisp") :output :string)))
(handler-case
 (progn (topic-maps.ctm:deserialize-string
          "%mergemap <child> <http://psi.topicmaps.org/iso13250/ctm>"
          :document-iri "urn:base-control")
        (error "Base unexpectedly accepted Mergemap"))
 (topic-maps.ctm:ctm-unsupported (c)
  (assert (eq :unsupported-directive (topic-maps.ctm:error-code c)))
  (format t "BASE-CONTROL-PASS: committed lexer rejected Mergemap as ~A~%" c)))
""".replace('BASE:', BASE + ':')
    probe = subprocess.run(['/usr/bin/sandbox-exec', '-p', POLICY, runtime, '--noinform', '--no-sysinit',
                            '--no-userinit', '--script', '/dev/stdin'], input=control.encode(), cwd=ROOT,
                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    (output / 'base-control.log').write_bytes(probe.stdout)
    assert probe.returncode == 0, 'Base lexer control failed'
    for path, value in protected.items():
        assert sha(ROOT / path) == value, path
    assert not subprocess.check_output(['git', 'diff', BASE, '--', 'vendor', 'references', 'src/tmdm', 'src/cxtm'], cwd=ROOT)
    for path, value in sources.items():
        assert sha(ROOT / path) == value, path
    previous_totals = json.loads((old / 'execution.json').read_text())['regressionChecks']
    totals = {marker: int(re.search(r'(?m)^' + marker + r'-PASS: (\d+)', log).group(1))
              for marker in [*previous_totals, 'MERGEMAP']}
    assert {k: totals[k] for k in previous_totals} == previous_totals
    evidence = dict(baseCommit=BASE, branch='codex/ctm-slice-19b', runtime=version,
                    startedAtUTC=started, finishedAtUTC=datetime.datetime.now(datetime.timezone.utc).isoformat(),
                    command=command, exitCode=result.returncode, freshSBCL=True, network='denied by sandbox-exec',
                    regressionChecks=totals, totalLispChecks=sum(totals.values()), importedStatuses=now['statuses'],
                    importedStatusChanges=changes, retainedOriginalCXTMOutputsAndModelObservationsUnchanged=True,
                    originalCorpusAndReferencesUnchanged=True, tmdmAndCxtmSourcesUnchanged=True,
                    documentedInspectionPassed=True, baseLexerCounterprobePassed=True, sourceSHA256=sources,
                    qualification='CTM Mergemap via an explicit finite bytes resolver and Slice 19A import. Four positives and three intended rejections newly executed. Include and XTM remain NOT-RUN; two DateTime FAILs unchanged.',
                    artifactsSHA256={str(p.relative_to(output)): sha(p) for p in sorted(output.rglob('*'))
                                     if p.is_file() and p.name != 'execution.json'})
    (output / 'execution.json').write_text(json.dumps(evidence, ensure_ascii=False, indent=2) + '\n')
    print(json.dumps({k: evidence[k] for k in ['runtime', 'regressionChecks', 'totalLispChecks', 'importedStatuses']}, sort_keys=True))
    print('SLICE-19B-VALIDATION-PASS: fresh offline regression; four byte-exact Mergemap originals and three intended rejections; prior results unchanged')


if __name__ == '__main__':
    main()
