#!/usr/bin/env python3
"""render-kb-block: write the model ranking into a KB page as a generated block, never by hand.

11-19: a hand-written ranking on the KB page "AI Model Leaderboard - Agentic Work and Cheap Tier" relayed a
laptop bench finding as guidance. The page now carries only this block for the ranking, rendered from
LEADERBOARD.md (the scorer's export): per role, everyday rate and n first, the bench in its own column.
kb-rank-lint checks the rest of the page.

  render-kb-block --print                    print the block
  render-kb-block PAGE.md                    replace the block between the markers in PAGE.md (exit 2 if absent)
  --leaderboard PATH (default: LEADERBOARD.md next to this folder), --top N routed rows per role (default 5)
"""
import argparse
from pathlib import Path
import re
import sys

BEGIN, END = '<!-- rstack:leaderboard:begin -->', '<!-- rstack:leaderboard:end -->'
HERE = Path(__file__).resolve().parent


def rows(text):
    """The newest date's everyday-table rows (7 columns) per role, in file order."""
    found = []
    for line in text.splitlines():
        cells = [c.strip() for c in line.strip().strip('|').split('|')]
        if line.startswith('| ') and len(cells) == 7 and cells[0] != 'Role' and re.fullmatch(r'\d{4}-\d\d-\d\d', cells[6]):
            found.append(cells)
    latest = {}
    for cells in found:
        latest[cells[0]] = max(latest.get(cells[0], ''), cells[6])
    return [c for c in found if c[6] == latest[c[0]]]


def block(text, top=5):
    table = rows(text)
    if not table:
        raise ValueError('no everyday routing table in the leaderboard')
    date = max(c[6] for c in table)
    out = [BEGIN,
           '_Generated by rstack `leaderboard/render-kb-block` from LEADERBOARD.md ({}); the nightly rewrites it, so '
           'do not edit it by hand. Rank = graded everyday delegated work (pass, partial, fail). "n<5" ranks below '
           'every model with data. Bench is display only: it never sets a rank, an effort or a route. A dash in # '
           'is a model with a record that is not routed._'.format(date), '',
           '| Role | # | Model | Everyday passed | Everyday n | Bench passed |', '|---|---:|---|---:|---|---:|']
    for role, model, order, everyday, n, bench, _ in table:
        routed = order.isdigit() and int(order) <= top
        historical = order == '-' and everyday not in ('n<5', '-')
        if routed or historical:
            out.append('| {} | {} | {} | {} | {} | {} |'.format(role, order, model, everyday, n, bench))
    out.append(END)
    return '\n'.join(out) + '\n'


def replace(page_text, new_block):
    start, end = page_text.find(BEGIN), page_text.find(END)
    if start < 0 or end < start:
        raise ValueError('the page has no {} ... {} markers'.format(BEGIN, END))
    return page_text[:start] + new_block.rstrip('\n') + page_text[end + len(END):]


def main(argv=None):
    p = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    p.add_argument('page', nargs='?', type=Path)
    p.add_argument('--print', action='store_true')
    p.add_argument('--leaderboard', type=Path, default=HERE.parent / 'LEADERBOARD.md')
    p.add_argument('--top', type=int, default=5)
    a = p.parse_args(argv)
    try:
        new = block(a.leaderboard.read_text(), a.top)
        if a.print or not a.page:
            sys.stdout.write(new)
            return 0
        text = a.page.read_text()
        updated = replace(text, new)
        if updated != text:
            a.page.write_text(updated)
        return 0
    except (OSError, ValueError) as exc:
        print('render-kb-block: ' + str(exc), file=sys.stderr)
        return 2


if __name__ == '__main__':
    sys.exit(main())
