"""page_config.py - what the site's page tools need to know about paperclip_python.html.

It is read by tools in python/video_gen:

    run_page_files.py   runs STEPS and CHECKS, and keeps what each printed
    page_claims.py      compares the page's sentences with those outputs

Nothing here runs by itself. Before a run, Paperclip must be running on
127.0.0.1:3100 with NO company named "Three Geckos Noodles" in it: start it with
a new --data-dir. The steps number their tasks THR-1 to THR-4, in this order.
"""
LIBRARIES = ["requests", "ddgs"]
# The page's own files, in the order the page runs them.
STEPS = {
    "hello": ["hello.py"],
    "company": ["company.py"],
    "cashier_alone": ["cashier.py", "2 khao soi, 1 thai iced tea"],
    "cashier_nothing": ["cashier.py"],
    "hire": ["hire.py"],
    "order": ["order.py", "2 khao soi, 1 thai iced tea"],
    "order_pizza": ["order.py", "1 pad thai, 2 pizza"],
    "correct": ["correct.py", "THR-2", "1 pad thai, 2 water"],
    "runs": ["runs.py"],
    "race": ["race.py", "3 nam ngiao"],
    "budget": ["budget.py", "100"],
    "spend1": ["spend.py"],
    "spend2": ["spend.py"],
    "spend3": ["spend.py"],
    "order_paused": ["order.py", "2 water"],
    "budget_raised": ["budget.py", "1000"],
    "correct_paused": ["correct.py", "THR-4", "2 water"],
    "report": ["report.py"],
    # Tutorial 5. These two need Ollama running with the model qwen2.5:3b, and the
    # ddgs library. They make a company of their own, Research Desk.
    "paper": ["paper.py", "paper.txt"],
    "paper_options": ["paper.py", "paper.txt", "--template", "model_paper.txt",
                      "--target", "85"],
    # The same again with a model about five times the size (9 GB): slow on a laptop.
    "paper_big": ["paper.py", "paper.txt", "--template", "model_paper.txt",
                  "--model", "qwen2.5:14b"],
}
# Scripts in this folder. Each makes a company of its own and removes it again.
# install_size.py takes the data folder as an argument, so it is run by hand.
CHECKS = ["what_it_gets", "same_title", "race_many", "two_budgets", "wake_times",
          "long_text"]

WAKE = "checks/wake_times_output.txt"
GETS = "checks/what_it_gets_output.txt"
SIZE = "checks/install_size_output.txt"

# A measured number in a sentence is <span data-n="name">. Each name is tied here
# to an output file and to the words around the number in that file.
NUMBERS = {
    "first_s": ("order_output.txt", r"Status: done \(after ([\d.]+) seconds\)"),
    "paused_s": ("order_paused_output.txt", r"Status: blocked \(after ([\d.]+) seconds\)"),
    "wake_orders": (WAKE, r"^(\d+) orders"),
    "wake_fast": (WAKE, r"Fastest: ([\d.]+) seconds"),
    "wake_median": (WAKE, r"Median: ([\d.]+) seconds"),
    "wake_slow": (WAKE, r"Slowest: ([\d.]+) seconds"),
    "variables": (GETS, r"^(\d+) variables"),
    "program_mb": (SIZE, r"The program: ([\d,]+) MB"),
    "packages": (SIZE, r"(\d+) folders in node_modules"),
    "log_events": ("report_output.txt", r"^(\d+) things are in the activity log"),
    "runs_listed": ("runs_output.txt", r"^(\d+) runs, the newest first"),
    "paper_s": ("paper_output.txt", r"That took (\d+) seconds"),
    "options_s": ("paper_options_output.txt", r"That took (\d+) seconds"),
    "big_s": ("paper_big_output.txt", r"That took (\d+) seconds"),
    "list_chars": ("checks/long_text_output.txt",
                   r"in the list of tasks: ([\d,]+) characters"),
}

RACE = "checks/race_many_output.txt"
BUDGETS = "checks/two_budgets_output.txt"
TITLE = "checks/same_title_output.txt"

PAPER = "paper_output.txt"
OPTIONS = "paper_options_output.txt"
BIG = "paper_big_output.txt"

# (the output file, words that the page quotes from it)
QUOTED = [
    ("hello_output.txt", "Paperclip says: ok"),
    ("hello_output.txt", "Version: 2026.1005.0"),
    ("hello_output.txt", "Mode: local_trusted"),
    ("company_output.txt", "Its tasks will be numbered THR-1, THR-2, ..."),
    ("cashier_alone_output.txt", "- Total: 150 baht"),
    ("cashier_nothing_output.txt", "Give me an order to add up, or let Paperclip start me."),
    ("hire_output.txt", "        Cashier (general)"),
    ("order_output.txt", "Created THR-1: Bill: 2 khao soi, 1 thai iced tea"),
    ("order_output.txt", "A comment, written by the agent:"),
    ("order_pizza_output.txt", "I do not know the price of pizza."),
    ("correct_output.txt", "THR-2 is blocked."),
    ("correct_output.txt", "- Total: 80 baht"),
    ("runs_output.txt", "started by: assignment"),
    ("runs_output.txt", "started by: automation"),
    ("runs_output.txt", "THR-2 done"),
    ("race_output.txt", "Cashier asks for it. Paperclip answers 200"),
    ("race_output.txt", "Cashier 2 asks for it. Paperclip answers 409"),
    ("race_output.txt", "Issue checkout conflict"),
    ("race_output.txt", "THR-3 is done. It was done by Cashier."),
    ("spend1_output.txt", "Spent this month: 40 of 100 cents."),
    ("spend2_output.txt", "Spent this month: 80 of 100 cents."),
    ("spend3_output.txt", "Spent this month: 120 of 100 cents."),
    ("spend3_output.txt", "The cashier is paused."),
    ("spend3_output.txt", "Reason: budget"),
    ("order_paused_output.txt", "A comment, written by the system:"),
    ("order_paused_output.txt", "the original assignee is not invokable"),
    ("budget_raised_output.txt", "The cashier is idle."),
    ("correct_paused_output.txt", "- Total: 20 baht"),
    (RACE, "Round 5: 20 requests at once. Answers: {200: 1, 409: 19}"),
    (BUDGETS, "PATCH /agents/{id}: budget 100, spent 120, status idle, pause reason None"),
    (BUDGETS, "PATCH /agents/{id}/budgets: budget 100, spent 120, status paused"),
    (TITLE, "Same title, same agent, the first still open: the same task came back"),
    (TITLE, "Same title, same agent, after the first was cancelled: a new task was made"),
    (GETS, "Missing ones that cashier.py reads: none"),
    (TITLE, "could not delete Check same title: 500"),
    ("start_output.txt", "Deploy           local_trusted (private)"),
    ("start_output.txt", "Bind             loopback (127.0.0.1)"),
    (GETS, "Paperclip 2026.1005.0, mode local_trusted"),
    ("cashier.py", '"khao soi": 60'),
    ("cashier.py", "PAPERCLIP_RUN_ID"),
    # Tutorial 5: the grades of each run, and the words the page quotes from the papers.
    (PAPER, "Hired Writer."),
    (PAPER, "Hired Reviewer."),
    (PAPER, "Round 1. Score: 45"),
    (PAPER, "Round 2. Score: 45"),
    (PAPER, "Round 3. Score: 55"),
    (PAPER, "Best score: 55, in round 3 of 3. The target was 85."),
    (PAPER, "The notes from the web provide further insights"),
    (PAPER, "the cost of writing tests first is not addressed in the notes"),
    (PAPER, "guide to software development costs"),
    (OPTIONS, "Created RES-9"),
    (OPTIONS, "Round 1. Score: 55"),
    (OPTIONS, "Round 2. Score: 40"),
    (OPTIONS, "Round 3. Score: 40"),
    (OPTIONS, "Best score: 55, in round 1 of 3. The target was 85."),
    (OPTIONS, "So we should write the tests first on every project."),
    (BIG, "Round 1. Score: 50"),
    (BIG, "Round 2. Score: 85"),
    (BIG, "Best score: 85, in round 2 of 2. The target was 85."),
    (BIG, "\nTHE PAPER\n"),
    (BIG, "improves the effectiveness of unit test cases"),
    (BIG, "AirWatch"),
    (BIG, "According to a case study by Accedia (Sep 8, 2024)"),
    (BIG, "trial of TDD on one project for one quarter"),
    ("paper_agent.py", "REVIEW = "),
    ("paper.py", '"--template"'),
]


def more_claims(text_of):
    """The claims that need more than "these words are in that file"."""
    claims = []
    rounds = [line for line in text_of(RACE).splitlines() if line.startswith("Round")]
    claims.append(("race_many: five rounds, and in each exactly one request got the task",
                   len(rounds) == 5 and all("{200: 1, 409: 19}" in line for line in rounds),
                   str(rounds)))
    runs = text_of("runs_output.txt")
    listed = [line for line in runs.splitlines() if line.startswith("    ")]
    claims.append(("runs: every listed run succeeded with exit code 0",
                   bool(listed) and all("succeeded" in line and "exit code: 0" in line
                                        for line in listed), str(listed)))
    report = text_of("report_output.txt")
    claims.append(("report: no task is left open, in progress or blocked",
                   "'open': 0, 'inProgress': 0, 'blocked': 0, 'done': 4" in report,
                   report[:200]))
    files = ["hello.py", "company.py", "cashier.py", "hire.py", "order.py", "correct.py",
             "runs.py", "race.py", "budget.py", "spend.py", "report.py"]
    longest = max(len(text_of(name).splitlines()) for name in files)
    claims.append(("no file on the page is longer than 100 lines", 0 < longest <= 100,
                   f"{longest} lines"))
    return claims
