"""Inventory of rows for the v3.0 coding (SCHEME.md §7). Reads each source's row list from the file where it was sealed or ledgered;
writes rows.inventory.json. No coding here."""
import pathlib as _pl, sys as _sy; _sy.path.insert(0, str(next(p for p in _pl.Path(globals().get("__file__") or _pl.Path.cwd() / "_").resolve().parents if (p / "paths.py").exists()))); from paths import REVFIRST as _RF  # where the corpora are: see paths.py
import json, re, ast, pathlib
H = pathlib.Path(__file__).resolve().parent; UM = H.parent; NB = pathlib.Path(_RF+'/notebook/scripts')
def lit(path, name):
    src = pathlib.Path(path).read_text(encoding='utf-8'); m = re.search(r'^' + name + r'\s*=\s*\[', src, re.M)
    i = m.end() - 1; depth = 0
    for j in range(i, len(src)):
        c = src[j]
        if c == '[': depth += 1
        elif c == ']':
            depth -= 1
            if depth == 0: return ast.literal_eval(src[i:j + 1])
rows = []
for i, r in enumerate(json.load(open(UM / 'map/rev-matthew-ledger.json'))['rows']):
    rows.append({'id': f'REV{i+1:03d}', 'source': 'Revelation', 'matt': r['matt'], 'src_ref': 'Rev ' + r['rev'], 'src_text': r.get('rev_relation'),
                 'matt_text': r.get('matt_relation'), 'move': r.get('move'), 'lemmas': r.get('lemmas'), 'record': 'map/rev-matthew-ledger.json'})
for i, r in enumerate(lit(NB / 'bh.py', 'ROWS')):
    rows.append({'id': f'LET{i+1:03d}', 'source': 'letters', 'matt': r[0], 'src_ref': '; '.join(f'{b} {s}' for b, s in r[1]), 'src_text': r[3], 'matt_text': None,
                 'three_way': bool(r[2]), 'record': 'notebook/scripts/bh.py'})
for i, r in enumerate(lit(NB / 'bj.py', 'ROWS')):
    rows.append({'id': f'JOS{i+1:03d}', 'source': 'Josephus', 'matt': r[0], 'src_ref': r[1], 'src_text': r[4], 'kind': r[3], 'record': 'notebook/scripts/bj.py'})
k = 0
for f in ('rows_sealed.py', 'rows2_sealed.py'):
    for r in lit(UM / 'dss' / f, 'ROWS'):
        k += 1; rows.append({'id': f'DSS{k:03d}', 'source': 'scrolls', 'matt': r[0], 'src_ref': f'{r[1]} {r[2]}', 'src_text': r[-1], 'kind': r[5], 'record': 'dss/' + f})
for i, r in enumerate(lit(UM / 'scripture/x1.py', 'EXPL')):
    rows.append({'id': f'LXN{i+1:03d}', 'source': 'Septuagint, named', 'matt': r[0], 'src_ref': f'{r[1]} {r[2]}:{r[3]}', 'record': 'scripture/x1.py EXPL'})
for i, r in enumerate(lit(UM / 'scripture/x1.py', 'TAC')):
    rows.append({'id': f'LXT{i+1:03d}', 'source': 'Septuagint, tacit', 'matt': r[0], 'src_ref': f'{r[1]} {r[2]}:{r[3]}', 'cls': r[4], 'record': 'scripture/x1.py TAC'})
PHILO = [  # The Unfolding Map, 2AW.3 (sealed N1-N7, with their results) and 2AU.3 (a)-(c) read; (d) and (e) were not counted there and are not entered
 ('PHI001', '4:4', 'Leg. 3.174-176 with Rev 2:17', 'the manna by Deut 8:3: every word from the mouth of God', 'held'),
 ('PHI002', '10:34-35', 'Her. 130, 140, 225 with Rev 1:16; 2:12', 'the Logos the cutter; the sword; to divide in two', 'held'),
 ('PHI003', '17:2', 'Fug. 110 with Rev 1:13, 16', 'the head shone round with light; the garments', 'held'),
 ('PHI004', '2:2; 2:9', 'Conf. 62-63 with Rev 22:16; 16:12', 'a man whose name is Rising (anatole)', 'held, modest'),
 ('PHI005', '1:21', 'Mut. 121 with Rev 2:17; 19:12-13', 'Jesus, salvation of the Lord', 'held'),
 ('PHI006', '1:23; 28:20', 'Mut. 265 with Rev 21:3', 'the Lord in us (Num 14:9)', 'weak'),
 ('PHI007', '5:8-9', 'Conf. 145-146 with Rev 3:14; 21:7', 'called sons of God; the one who sees, Israel', 'held'),
 ('PHI008', '5:34-37', 'Spec. 2.2, 2.5; Decal. 84 with Rev 10:6', 'let the word be an oath; not by heaven, earth', 'found'),
 ('PHI009', '5:8', 'Philo passim with Rev 22:4', 'the one who sees God', 'read'),
 ('PHI010', '5:9', 'Spec. 2.192', 'the peacemaker God', 'read')]
for i, ref, txt, st in [(p[0], p[2], p[3], p[4]) for p in PHILO]:
    m = [p[1] for p in PHILO if p[0] == i][0]
    rows.append({'id': i, 'source': 'Philo, with Revelation', 'matt': m, 'src_ref': ref, 'src_text': txt, 'standing': st, 'record': 'The Unfolding Map 2AW.3, 2AU.3'})
json.dump(rows, open(H / 'rows.inventory.json', 'w'), ensure_ascii=False, indent=0)
import collections; print(collections.Counter(r['source'] for r in rows), len(rows))
