# Buying for several draws (#10)


## Configuration

`public.*` is still the **main draw**, so an old `config.json` works the same. `public.draws` is the list of **future draws already announced**:

```json
"draws": [
  { "id": "SEM-02", "title": "⚡ POLLA · Week 2", "price": 1000,
    "closeAt": "2026-11-27T19:00:00Z", "drawAt": "2026-11-27T20:00:00Z" },
  { "id": "NAVIDAD26", "title": "🎄 Christmas Draw", "price": 5000, "tickets": 10000,
    "closeAt": "2026-12-22T09:00:00Z", "drawAt": "2026-12-22T10:00:00Z", "image": "img/tickets/NAVIDAD26.jpg",
    "i18n": { "es": { "title": "🎄 Sorteo de Navidad" } } }
]
```

- Each draw carries its `id` (unique), `title`, `price`, `closeAt` and `drawAt`. Optionally, `tickets`, `image` (see ticket, #11) and `i18n` (texts in other languages).
- Any missing field is inherited from the main draw. Payments, theme, reservation minutes, confirmations and contact key are shared.
- In the panel, **Events → «➕ New event»** creates one in a single step (23/11/2026):
  - Choose the game: **one-number draw** or **LOTTO21 · 4 of 21**. The calendar type and the result mode are not asked: LOTTO21 is `weekly`, a test is `test`.
  - «It is a test (MAINNET TEST)»: manual result set in «Results and claims», sales close 5 minutes before, title with «MAINNET TEST» (the server requires it).
  - Otherwise the result is the first Bitcoin block after the time and sales close 4 h before (OpenTimestamps seal).
  - Id (`LOTTO21-01`, `TEST-DRAW-01`…) and date are suggested (LOTTO21: 7 days after the last one). LOTTO21 asks for the initial contribution (0 = nothing to pay).
  - «Create and save to the server» reads the server's config, adds **only that event** and saves it (NIP-07 signature). With a contribution, the panel then shows its invoice.
  - Errors show up under the button before signing, and the server's answer in the same place.
- Existing events are edited below, in «Upcoming announced draws», and saved with «Save events to the server». Fields you did not touch are sent back exactly as stored: re-sending a rounded date or 6 confirmations instead of 3 made the server refuse every save once an event had invoices.
- Before publishing it checks that the ids are valid and not repeated, that the price is greater than 0 and that the close is not after the block.
- A draw that already has sales must not be deleted. The panel warns if one is removed.

## Public website

- It shows draw `?draw=ID` or, if none is given, the next one to be held. If all have passed, it shows the last one.
- The «Announced draws» card lists them all and links to each one.
- In the cart, «Play the same numbers also in…» lets you choose the other open draws with the same number of numbers. They can be ticked one by one or with the «+1», «+3» and «All» buttons. The maximum is 8 draws per purchase.
- **The price is the sum**: number of numbers × (price of each chosen draw). It is paid with **a single invoice**.
- **Number taken in one of the draws**:
  - The board stripes the numbers free in this draw that are taken in another of the chosen ones.
  - When adding one of those, the website warns and does not put it in the cart.
  - If a number in the cart becomes taken when ticking another draw, «⚠ DRAW» appears next to the number.
  - Before creating the invoice every draw is checked. If there is a clash, nothing is charged and it says which number is taken and in which draw.
  - After reserving, it is checked again in all of them. The invoice is only shown if the reservation is yours in every draw.
- The Nostr reservation is a single event with one `t` tag per draw. The invoice expiry never goes beyond the earliest close of the chosen draws.

## Validation (each draw separately)

- The invoice carries the draws with the price the buyer saw: `@2|SEM-01:1000+SEM-02:1000|…` (see [claim-log.md](claim-log.md)).
- Required amount = number of numbers × sum of the declared prices.
- Each draw also requires that **its** declared price is not lower than the configured one. That way nobody pays less by declaring a low price in another draw, and a purchase still validates even if a draw already held leaves the configuration.
- Pot of each draw = its valid numbers × its price.

### Refunds

| Case | What is refunded | Scope |
|---|---|---|
| Insufficient amount or expired reservation | the whole payment, only once | purchase |
| Paid after a draw's close | only that draw's part | draw |
| Double payment of a number in a draw | that number × that draw's price; it keeps playing in the others | draw |
| Lower declared price in a draw | only that draw's part | draw |

`mergeRefunds()` merges the refunds of several draws without repeating those that affect the whole purchase. The panel's «Ledger of all draws (CSV)» button uses it.

## Panel

- The «Draw» selector (shown when there is more than one) switches sales, statistics, ledger and results to the chosen draw.
- «Backups → Ledger of all draws (CSV)» has one line per number and draw for every purchase. Multiple purchases are recognised by `purchase_id`, `draws_in_purchase` and `position`.
