#!/usr/bin/env python3
"""strings.gen.ts vs the official sources at both commits. Python `ast` on text extracted with git show; nothing is imported or run.
argv: <repo> <strings.gen.ts>"""
import ast, hashlib, json, re, subprocess, sys

repo, gen = sys.argv[1], sys.argv[2]
src = open(gen, encoding='utf-8').read()


def block(name):
    return json.loads(re.search(r'export const ' + name + r' = (.*?) as const;', src, re.S).group(1))


def show(c, p):
    return subprocess.run(['git', '-C', repo, 'show', f'{c}:{p}'], capture_output=True).stdout


SRC = block('SOURCES')
for k, v in SRC.items():
    h = hashlib.sha256(show(v['commit'], v['path'])).hexdigest()
    print('source', k, v['path'], 'sha256 match', h == v['sha256'])


def critic(c):
    t = ast.parse(show(c, 'gca_evaluation/prompts.py').decode())
    for n in t.body:
        if isinstance(n, ast.Assign) and n.targets[0].id == 'critic_prompts':
            return ast.literal_eval(n.value)


J = block('JUDGE')
for c in ('93c1474', '624efc5'):
    cp = critic(c)
    print(c, 'template equal', cp['self-play-deduct-template'] == J['template'], 'details equal', {d: cp['dimension_details'][d] == J['details'][d] for d in J['dimensions']}, 'prompts keys', list(cp))


def fstr(c, func):
    """Literal parts of the f-strings assigned to `prompt` in a function."""
    t = ast.parse(show(c, 'gca_evaluation/utils.py').decode())
    for n in t.body:
        if isinstance(n, ast.FunctionDef) and n.name == func:
            for s in n.body:
                if isinstance(s, ast.Assign) and getattr(s.targets[0], 'id', '') == 'prompt':
                    parts = []
                    for v in s.value.values:
                        parts.append(v.value if isinstance(v, ast.Constant) else '{' + ast.unparse(v.value) + '}')
                    return ''.join(parts)


def fl(parts):
    return ''.join(p if isinstance(p, str) else '{' + p['v'] + '}' for p in parts)


print('env prompt 93c1474 == 624efc5', fstr('93c1474', 'get_environment_prompt') == fstr('624efc5', 'get_environment_prompt'))
print('env prompt gen == 624efc5', fl(block('ENVIRONMENT_PROMPT')['prompt']) == fstr('624efc5', 'get_environment_prompt'))
print('nsp prompt gen == 624efc5', fl(block('NSP_PROMPT')['prompt']) == fstr('624efc5', 'get_nsp_prompt'))
print('paper-era nsp gen == 93c1474', fl(block('PAPER_ERA_NSP_PROMPT')['prompt']) == fstr('93c1474', 'get_nsp_prompt'))
cp = block('CHARACTER_PROMPT')
for c in ('93c1474', '624efc5'):
    text = show(c, 'gca_evaluation/utils.py').decode()
    print(c, 'outputFormat literals present', {k: v in text for k, v in cp['outputFormat'].items()})
    print(c, 'fixed template markers present', all(m in text for m in ["===Requirements===", "===Your Inner Thoughts===", "===Information about the other Characters===", "===Current Scenario==="]))
ag = block('AGENT')
for c in ('93c1474', '624efc5'):
    text = show(c, 'gca_evaluation/agent.py').decode()
    print(c, '60-word line present', 'Limit your response to 60 words' in text, 'max_tokens=512 present', 'max_tokens=512' in text)
