"""Cost of 10,000 pages from formulas (docs.pinecone.io/guides/cost/understand-cost, pinecone.io/pricing, read 2026-11-06)
and ratios measured on this corpus. Nothing here is loaded into Pinecone."""
import json

import common

rows, _, _ = common.read_chunks()
docs = [common.to_doc(r) for r in rows]
n, pages = len(docs), len({d["url"] for d in docs})
cpp = n / pages
CHUNKS_10K = round(10_000 * cpp)

mean = lambda f: sum(f(d) for d in docs) / n
text_b = mean(lambda d: len(d["chunk_text"].encode()))
meta_b = mean(lambda d: sum(len(d[k].encode()) for k in ("url", "heading", "section")))
id_b = mean(lambda d: len(d["_id"]))
DIM = 1024
formula_doc = id_b + meta_b + text_b + DIM * 4
measured_doc = json.load(open("/data/load_stats.json"))["size_bytes"] / n
print(f"corpus: {n} chunks, {pages} pages -> {cpp:.2f} chunks per page -> 10,000 pages = {CHUNKS_10K:,} chunks")
print(f"mean bytes per chunk: id {id_b:.0f} + metadata {meta_b:.0f} + text {text_b:.0f} + vector {DIM}x4={DIM * 4}")
print(f"  formula: {formula_doc:,.0f} B per document; measured by describe_namespace: {measured_doc:,.0f} B per document\n")

# measured embedding tokens per chunk, three ways of batching (load.out, token_count_full.out)
TOK = {"one chunk per request": 46187 / n, "sorted by length, 96 per request": 67344 / n, "file order, 96 per request (load.py)": 121938 / n}

# rates, Standard plan, read 2026-11-06
GB_MONTH, WU_M, RU_M, EMB_M = 0.33, (4.00, 4.50), (16.0, 18.0), 0.16

size_gb = CHUNKS_10K * measured_doc / 1e9
print("STORAGE   size = docs x (id + metadata + text + dims x 4 B)")
print(f"          {CHUNKS_10K:,} x {measured_doc:,.0f} B = {size_gb:.3f} GB -> x $0.33 = ${size_gb * GB_MONTH:.2f} per month (Starter cap 2 GB, Builder 10 GB)\n")

req_b = text_b + meta_b + id_b
wu_low, wu_high = CHUNKS_10K * req_b / 1024, CHUNKS_10K * measured_doc / 1024
print("WRITES    upsert = 1 WU per KB of request, plus 1 WU per KB of an existing record it overwrites")
print(f"          first load: {wu_low:,.0f} WU if the request counts only text + metadata ({req_b:.0f} B per doc), "
      f"{wu_high:,.0f} WU if it counts the full stored document")
print(f"          = ${wu_low / 1e6 * WU_M[0]:.2f} to ${wu_high / 1e6 * WU_M[1]:.2f} (Starter cap 2M per month)")
rc = round(CHUNKS_10K * 0.10)
rc_wu = rc * (req_b + measured_doc) / 1024
print(f"          re-crawl of 10% of the pages: {rc:,} chunks overwritten -> request + existing record = about {rc_wu:,.0f} WU per re-crawl\n")

print("EMBEDDING tokens for the first load, x $0.16 per 1M tokens (Starter 5M per month, Builder 10M, per model)")
for k, v in TOK.items():
    t = CHUNKS_10K * v
    print(f"          {k:38s} {v:6.1f} tokens/chunk -> {t / 1e6:5.2f}M tokens -> ${t / 1e6 * EMB_M:5.2f}; "
          f"at 250,000 tokens per minute: {t / 250_000:5.0f} min")
print()

Q = 100_000
q_tok = 15
print(f"READS     {Q:,} searches per month, namespace of {size_gb:.2f} GB")
print(f"          formula: max(0.25, {size_gb:.2f}) RU per search = {Q * max(0.25, size_gb):,.0f} RU -> ${Q * max(0.25, size_gb) / 1e6 * RU_M[0]:.2f} to ${Q * max(0.25, size_gb) / 1e6 * RU_M[1]:.2f}")
print(f"          if every search is billed the 1 RU the response reports: {Q:,} RU -> ${Q / 1e6 * RU_M[0]:.2f} to ${Q / 1e6 * RU_M[1]:.2f}")
print(f"          query embedding: {Q:,} x ~{q_tok} tokens = {Q * q_tok / 1e6:.1f}M tokens -> ${Q * q_tok / 1e6 * EMB_M:.2f}\n")

lo = size_gb * GB_MONTH + wu_low / 1e6 * WU_M[0] + CHUNKS_10K * TOK["one chunk per request"] / 1e6 * EMB_M + Q * max(0.25, size_gb) / 1e6 * RU_M[0] + Q * q_tok / 1e6 * EMB_M
hi = size_gb * GB_MONTH + wu_high / 1e6 * WU_M[1] + CHUNKS_10K * TOK["file order, 96 per request (load.py)"] / 1e6 * EMB_M + Q / 1e6 * RU_M[1] + Q * q_tok / 1e6 * EMB_M
print(f"TOTAL     first month of usage at Standard rates: ${lo:.2f} to ${hi:.2f}; the plan minimum is $50 (Standard) or $20 flat (Builder)")
