"""Every figure in "Presto Reported 95% of Drive-Thru Orders Completed Without Intervention", checked against the two
primary documents' own text.

Inputs: put the two SEC PDFs in a sources/ directory next to this script, under these names:
  sources/sec_33-11352_presto.pdf     SEC order, In re Presto Automation Inc., Release 33-11352, 14 Jan 2025
                                      https://www.sec.gov/files/litigation/admin/2025/33-11352.pdf
  sources/sec_comp26282_saniger.pdf   SEC v. Saniger complaint, 25 Civ. 2937, S.D.N.Y., filed 9 Apr 2025
                                      https://www.sec.gov/files/litigation/complaints/2025/comp26282.pdf
The script extracts each PDF's text with page markers (sources/*.txt, needs `pip install pypdf`), asserts that every rate,
dollar and time figure the essay quotes is present VERBATIM on the page cited, and prints the derived numbers. A figure
that is not on its page fails the run.

Run: python figures_c6066.py
"""
import re
import sys
from pathlib import Path

HERE = Path(__file__).resolve().parent
SRC = HERE / "sources"


def pages(pdf_name):
    txt = SRC / (pdf_name.rsplit(".", 1)[0] + ".txt")
    if not txt.exists():
        import pypdf
        r = pypdf.PdfReader(str(SRC / pdf_name))
        txt.write_text("\n".join("=== PAGE %d ===\n%s" % (i, p.extract_text() or "") for i, p in enumerate(r.pages, 1)),
                       encoding="utf-8")
    out = {}
    parts = re.split(r"=== PAGE (\d+) ===\n", txt.read_text(encoding="utf-8"))
    for i in range(1, len(parts), 2):
        out[int(parts[i])] = re.sub(r"\s+", " ", parts[i + 1])
    return out


def on_page(doc, page, needle):
    if re.sub(r"\s+", " ", needle) not in doc[page]:
        sys.exit("MISSING on p.%d: %r" % (page, needle))
    return needle


def main():
    nate = pages("sec_comp26282_saniger.pdf")
    presto = pages("sec_33-11352_presto.pdf")

    # Nate: the complaint's own round figures (¶1 p.1, ¶41-42 p.7, ¶65 p.10, ¶66 p.10)
    on_page(nate, 1, "raised over $42 million")
    on_page(nate, 7, "Investor B invested $4 million")
    on_page(nate, 7, "Investor A invested $4 million")
    on_page(nate, 10, "approximately $34 million of Nate shares")
    on_page(nate, 10, "Saniger sold $3 million of his own Nate stock")
    seed, series_a = 4 + 4, 34
    print("Nate: named seed wires $%dM + Series A ~$%dM = ~$%dM (complaint ¶1 says 'over $42 million')"
          % (seed, series_a, seed + series_a))
    on_page(nate, 7, "success ranges from 93% to 97%")
    on_page(nate, 7, "its above 99% success")
    on_page(nate, 9, "essentially zero")
    on_page(nate, 6, "only 10 seconds")
    on_page(nate, 7, "virtually all orders placed by Nate")

    # Presto: offerings (¶15, ¶17 p.5) and the rates (¶26 p.7, ¶30 p.8, ¶38 p.9)
    on_page(presto, 5, "raised approximately $55.5 million")
    on_page(presto, 5, "approximately $49.8 million")
    on_page(presto, 5, "raised approximately $9.5 million")
    print("Presto: PIPE $55.5M + May 2023 private placement $9.5M = $%.1fM raised while the statements were live"
          % (55.5 + 9.5))
    on_page(presto, 6, "over 94% accuracy")
    on_page(presto, 8, "rates of 95% to 99%")
    on_page(presto, 8, "rates greater than 95%")
    on_page(presto, 7, "approximately 70% of the time")
    on_page(presto, 9, "on average 85%")
    on_page(presto, 9, "100% of orders")
    print("Presto pilot version: human agent entered ~70%% of orders -> AI completed at most ~%d%% alone" % (100 - 70))
    print("Presto original version (majority of locations): human agent intervention on 100% of orders -> AI alone 0%")
    print("Presto, 14 Dec 2023 8-K as the order reads it (¶38 p.9), ONE population (restaurants on Presto's own tech): "
          "'non-intervention' averaged 85% while the substantial majority of those locations needed a human agent on 100% "
          "of orders -> the gap between the two numbers is the off-site workforce the first one does not count")
    print("Together: Presto $%.1fM + Nate over $%dM = more than $%.1fM" % (55.5 + 9.5, seed + series_a, 55.5 + 9.5 + seed + series_a))
    print("ALL FIGURES FOUND ON THEIR CITED PAGES")


if __name__ == "__main__":
    main()
