#!/usr/bin/env python3
import json
BASE = "https://amberbadges.auravision.co"
members = json.load(open("/tmp/fork_members.json"))          # {num: {handle,kind}}
notes = json.load(open("/tmp/fork_notes.json"))              # {num: {title,note,...}} bespoke

# bespoke medallion files (reuse existing renders)
BESPOKE_IMG = {0:"amber_no_000.png",7:"amber_no_007_bond.png",19:"amber_no_019.png",
               21:"amber_no_021_blackjack.png",27:"amber_no_027_roulette.png",
               37:"amber_no_037_sheldon.png",47:"amber_no_047_ag.png",73:"amber_no_073_sheldon.png"}
IMG_VER = {47:3}  # 47 is on v3; everything else v2

STORY = {
 1:"One is where counting begins — and the number mathematics refuses to file. It is neither prime nor composite but the unit itself: the thing every integer is assembled from by addition, and the identity that leaves any number unchanged under multiplication. Every tower of mathematics rests on this single brick. In Club Amber there is only one No. 001, and it is the earliest seat any member can hold short of the founder's own. The first voice in the room.",
 2:"Two is the only even prime — the lone exception every rule about primes has to make room for. It is the base of binary, the on and the off from which every machine in the world is built, and the smallest step past unity. Mathematics is full of twos: the pair, the proof by two cases, the first division. In the Amber series it is the second number ever struck; after 000 and 001, there is no lower seat left to take.",
 3:"Three is the first odd prime and the first triangular number — one plus two, the smallest count that makes a shape. It is a Mersenne prime (2²−1), the dimensions we move through, and the figure liturgy and folklore return to endlessly: three wishes, three acts, three strikes. In Club Amber, No. 003 is struck in onyx — a single dark seat among the opening arrivals, low enough that everyone below it fits on one hand.",
 4:"Four is the first composite number and the first square beyond one: two twos, the corners of every room and the legs of every table. It is the cardinal directions, the seasons, the classical elements — the integer civilisation reaches for whenever it wants to name the whole of something. In the Amber series it sits among the earliest seats, a single-digit rarity from the founding days of the club.",
 5:"Five is the third prime and the hypotenuse of the first Pythagorean triple — 3² + 4² = 5², the right angle the ancients could tie into a knotted rope. It is a Fibonacci number, the fingers on a hand and the points of a star, struck here in deep green. Among the opening numbers of Club Amber, a single-digit seat carries a weight no later arrival can: it says you were early, and that almost no one was earlier.",
 6:"Six is the first perfect number — equal to the sum of its own divisors, 1 + 2 + 3 — a property so rare only a handful are known below a billion. It is the third triangular number and 3 factorial, the faces of a die and the symmetry of a snowflake. Mathematicians since Euclid have treated perfection as a genuine category; six is its first member. In Club Amber, likewise a single-digit seat from the founding run.",
 8:"Eight is the first cube beyond one — two to the third, a doubled line made solid — and a Fibonacci number besides. It is the base of octal, the bits in a byte, and, laid on its side, the sign for infinity. Cultures from Beijing to the baccarat table read it as the luckiest digit. In the Amber series it is a single-digit seat: struck early, held close, and all but impossible to find now the founding numbers are gone.",
 9:"Nine is three squared and the last of the single digits — the final figure before the decimal system folds back to ten. It carries the famous trick of digital roots: multiply nine by anything and the digits sum to nine again, forever. It is the muses, the innings, the lives of a cat. In Club Amber, No. 009 is one of the last single-digit seats, and the lower the number, the earlier the arrival — rarity measured in sequence, not supply.",
 10:"Ten is the base our whole civilisation counts in — the tally of our own fingers, raised to a system. It is the fourth triangular number (1 + 2 + 3 + 4), the tetractys the Pythagoreans held sacred, and the threshold where single digits give way to place value. Struck here in midnight blue. In the Amber series, No. 010 is the hinge: the last of the first ten, and the first of everything that came after.",
 11:"Eleven is the fifth prime and the first two-digit prime — the first repunit, a number written entirely in ones, and a palindrome that reads the same both ways. It is where most languages stop naming numbers as ten-and-a-digit and start again. Struck here in sunburst blue. In Club Amber it opens the two-digit seats: still low, still early, still among the first dozen voices the room ever heard.",
 12:"Twelve is the smallest abundant number — its divisors (1, 2, 3, 4, 6) overflow it — and one of the most divisible small integers there is, which is why the world keeps reaching for it: twelve months, twelve hours, twelve inches, a dozen. Where ten is the number we count with, twelve is the number we organise by. In the Amber series it closes the first twelve seats — the earliest cohort, struck in the founding days.",
 14:"Fourteen is a Catalan number — the fourth — counting the ways a sequence of choices can be bracketed, a figure that surfaces wherever combinatorics asks 'how many?'. It is twice seven, the days of a fortnight, the lines of a sonnet. In Club Amber, No. 014 sits among the early two-digit seats: low enough to mark its holder as one of the first to step into the room.",
 15:"Fifteen is the fifth triangular number — 1 through 5, stacked — and the magic constant of the smallest magic square, every row and column summing to it. It is 3 × 5, the first product of two distinct odd primes, and in binary a tidy 1111. In the Amber series it is an early seat from the founding run, its lowness the whole of its worth.",
 16:"Sixteen is the only number that is a perfect square and a perfect power two ways at once: 2⁴ and 4², the unique solution to aᵇ = bᵃ in distinct integers. It is the values of a hexadecimal digit and a quarter of a chessboard. A number mathematicians single out for a coincidence that happens exactly once. In Club Amber, an early two-digit seat, struck in the opening days.",
 17:"Seventeen is the seventh prime and a Fermat prime — 2⁴ + 1 — which is why a regular seventeen-sided polygon can be drawn with compass and straightedge alone, the proof a nineteen-year-old Gauss was so proud of he asked for the shape on his gravestone. It is also the sum of the first four primes (2 + 3 + 5 + 7). In the Amber series, an early seat whose number does the talking.",
 18:"Eighteen is the only number that is exactly twice the sum of its own digits (1 + 8 = 9; 9 × 2 = 18) — a small self-referential trick no other integer repeats. It is an abundant number, the age of majority across much of the world, and 2 × 3². In Club Amber it sits among the early two-digit arrivals, struck when the room was still small enough to know every voice in it.",
 20:"Twenty is the third tetrahedral number — stack the first four triangular numbers (1 + 3 + 6 + 10) and twenty spheres settle into a flawless pyramid. It is a score, the base of more than one ancient counting system, and 2² × 5. In the Amber series, a founding-band seat: reserved early, struck in the opening run, its low number a quiet proof of how soon its holder arrived.",
 22:"Twenty-two carries the most famous approximation in all of arithmetic: 22/7, the fraction still taught for π, accurate to better than a part in a thousand. It is a pentagonal number, 2 × 11, and the cards of the major arcana. In Club Amber it is an early founding seat — struck in slate, low enough to mark one of the room's first voices.",
 23:"Twenty-three is the ninth prime and the engine of the birthday paradox: gather just twenty-three people and it is already more likely than not that two share a birthday — one of probability's great affronts to intuition. A prime that recurs through cryptography and conspiracy alike. In the Amber series, an early founding seat, struck in the opening run.",
 24:"Twenty-four is four factorial — the arrangements of four things — and famously the only integer for which the squares 1² through 24² sum to a perfect square (70²), a coincidence that underpins deep results in lattice theory and string theory. It is the hours of the day and one of the most divisible small numbers. In Club Amber, an early founding seat from the opening run.",
 25:"Twenty-five is five squared and the sum of the first five odd numbers — as every square is the sum of consecutive odds. It is 3² + 4², the square of the first Pythagorean hypotenuse, and a silver quarter of a hundred. In the Amber series it is a founding-band seat: low, early, struck when the club was still counted in dozens.",
 26:"Twenty-six is the only number in all of mathematics wedged between a perfect square and a perfect cube — 25 = 5² beneath it, 27 = 3³ above — a solitude Fermat claimed and Euler proved. It is 2 × 13 and the letters of the Latin alphabet. In Club Amber, a founding seat holding a theorem no other integer can.",
 28:"Twenty-eight is the second perfect number — 1 + 2 + 4 + 7 + 14 — the rare company that six keeps, known since antiquity and still vanishingly scarce. It is the seventh triangular number and the days of a lunar month. In the Amber series it is a founding seat; where most numbers are merely low, this one is mathematically perfect.",
 29:"Twenty-nine is the tenth prime and the sum of three consecutive squares — 2² + 3² + 4² — a Lucas prime and the lower twin of 31. It is the day February borrows once every four years. In Club Amber, a founding-band seat from the opening run, its primality and its lowness both part of the draw.",
 30:"Thirty is the primorial of the first three primes — 2 × 3 × 5 — and the largest number for which every smaller number coprime to it is itself prime, a tidy property that runs out forever after. It is a human generation, a round of years, the degrees of a zodiac sign. Struck here in green. In the Amber series, a founding seat from the opening days.",
 31:"Thirty-one is the eleventh prime and a Mersenne prime — 2⁵ − 1 — and the number on which Club Amber built its founding tier: thirty-one seats, a nod to π's 3.1. It is the length of the longest months and a prime one step below the Sheldon twins. In the Amber series it is the ceiling of the founding band — struck in blue, the last of the first thirty-one.",
 32:"Thirty-two is two to the fifth — the fifth doubling, a five-bit world, the degrees at which water freezes on the older scale. It is the first number past the founding thirty-one, which gives it a quiet distinction: the first seat of the second cohort, struck only once the opening band had sold out. A hinge in the club's own history.",
 33:"Thirty-three made mathematical news in 2019, when it became the last number below a hundred proven to be a sum of three cubes — a hunt that took a planet's worth of computers and integers sixteen digits long. It is 3 × 11 and, in lore, a number of mastery. Struck here in oxblood. In the Amber series, an early second-cohort seat with a thoroughly modern claim to fame.",
 34:"Thirty-four is a Fibonacci number — the ninth — and the magic constant of the four-by-four square, every line summing to it, the figure Dürer hid in the corner of his Melencolia I. It is 2 × 17. In Club Amber it sits just past the founding band, an early member of the cohort that formed once the first thirty-one were gone.",
 35:"Thirty-five is the fifth tetrahedral number — the first five triangular numbers stacked into a pyramid — and 5 × 7, the product of the two primes that bracket six. It is the frames in a second of film and the number on a certain famous highway. In the Amber series, a second-cohort seat struck after the founding run closed.",
 36:"Thirty-six is both a perfect square and a perfect triangular number — 6² and the sum 1 through 8 — one of the very few integers that are both, and the sum of the first three cubes besides (1 + 8 + 27). It is a tenth of a circle in degrees and one of the most divisible small numbers. In Club Amber, a second-cohort seat with a double geometric pedigree.",
 37:"Thirty-seven is the twelfth prime and an emirp — reverse its digits and you reach 73, itself prime — one half of the only Sheldon prime pairing in mathematics. Psychologists have found it is the number people reach for most often when asked to 'pick a random number,' as though the mind itself finds it irreducibly arbitrary. In Club Amber its twin, No. 073, rests in the founder's own collection; this is its mirror, held quietly by an early member.",
 74:"Seventy-four is 2 × 37 and a sum of two squares — 7² + 5² — twice the lower of the Sheldon twins. It sits deep in the second cohort of Club Amber, among the seats struck once the founding and early numbers had all been claimed: a later arrival, but one from the opening season all the same.",
 75:"Seventy-five is 3 × 5² and three-quarters of a hundred — a diamond anniversary, an ordinal that still rings of milestone. In the Amber series it is a second-cohort seat, struck in the run that followed the founding band, its holder among the club's early hundred.",
 76:"Seventy-six is an automorphic number — square it and the result, 5776, ends in 76 itself, a number that keeps reproducing its own last digits forever. It is 2² × 19. In Club Amber, a second-cohort seat whose quiet arithmetic rewards the collector who looks twice.",
}

def status_of(n):
    if n in (0,31,73): return "founder"
    if n in (7,21,27,47): return "for sale"
    if n == 19: return "consigned"
    return "member"

def title_of(n):
    if n in notes and notes[str(n)].get("title"):
        return notes[str(n)]["title"]
    return f"No. {n:03d}"

def medallion_of(n):
    fn = BESPOKE_IMG.get(n, f"amber_no_{n:03d}.png")
    return f"{BASE}/{fn}?v={IMG_VER.get(n,2)}"

out = []
# grants give member numbers; 47 (Ag) is an auction lot with no grant, add it explicitly
allnums = sorted(set(int(k) for k in members) | {47})
for n in allnums:
    m = members.get(str(n), {})
    if str(n) in notes and notes[str(n)].get("note"):
        story = notes[str(n)]["note"]                      # bespoke, already multi-paragraph
    else:
        story = STORY.get(n, "")
        for pivot in ("In the Amber series", "In Club Amber", "In the Amber"):
            if " " + pivot in story:
                story = story.replace(" " + pivot, "\n\n" + pivot, 1)
                break
    out.append({
        "number": n,
        "title": title_of(n),
        "story": story,
        "medallion": medallion_of(n),
        "status": status_of(n),
        "handle": (m.get("handle") or "").strip(),
    })

missing = [c["number"] for c in out if not c["story"]]
json.dump({"coins": out}, open("rnd/area31/amber/collection.json","w"), indent=2, ensure_ascii=False)
print("coins:", len(out), "| with story:", sum(1 for c in out if c["story"]), "| missing story:", missing)
print("statuses:", {s: sum(1 for c in out if c['status']==s) for s in set(c['status'] for c in out)})
