> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Reward song

A locked song given for good after a finished song, up to a number a day. A/B test: `v0` (baseline) gives none, `v1` and `v2` differ only in the number. Code: [`src/features/song-reward`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/src/features/song-reward).

## Flow

1. A full Game Mode song ends and its result settles (`onRevealComplete`). Tutorial songs, Learn Mode and single-part runs do not count; a replay does.
2. `PlayResultScreen` calls `useSongRewardFlowStore.present()`. It counts the song, picks the next one, unlocks it (`unlockSongEntitlement`, source `promo`, both modes) and only then shows the progress.
3. The bar fills. When it is full the popup opens: **Play now** goes the way "Next song" does (ads rules included), **Later** closes it.
4. On that result only, the song leads the suggestions (`resultSongId` → `rewardSongId` in `pick-session-recommendations.ts`). Leaving the result clears it.
5. After "Later", or when there was nothing to give, the result's own paywalls and No Ads popup run as before. After "Play now" they stay due.

Nothing is shown when the day's songs are spent, the list has nothing left, or the group gives none. A failed unlock spends no turn.

## Remote Config

| Key                | Value                                                                                                                                           | Locked per install          |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- |
| `reward_song`      | `{"variant":"v1","max_songs":2,"songs_per_reward":1,"reset_at":"24:00"}`. Only `variant` is needed; `max_songs` defaults to 2 (`v1`) / 5 (`v2`) | Yes, with `ab_lock_version` |
| `reward_song_pool` | `{"songs":["rush-e","bella-ciao"]}`: catalog ids, given in this order                                                                           | No, read live               |

A song the player can already play, or that the app does not list, is passed over. Development builds: `EXPO_PUBLIC_RC_REWARD_SONG`, `EXPO_PUBLIC_RC_REWARD_SONG_POOL` in `.env.local`.

## The day

`logic/reward-window.ts`. The count starts over at `reset_at` on the player's own clock. That moment is kept as an absolute time, so changing the time zone does not end a day early, and two resets are never less than 23 hours apart, so hopping zones cannot chain short days. The device clock is trusted: setting it forward opens a day early.

### Testing the reset (development builds)

`EXPO_PUBLIC_REWARD_SONG_RESET_MINUTES` in `.env.local`, then restart Metro. A release build ignores it.

| Value                    | Reset                                                         | Clock  |
| ------------------------ | ------------------------------------------------------------- | ------ |
| `5` (any number above 0) | That many minutes after the first song finished in the period | Shown  |
| `0`                      | The real rule                                                 | Shown  |
| Unset or not a number    | The real rule                                                 | Hidden |

The clock (`SongRewardDebugClock`) sits at the top of every screen: the count, the time to the reset, the next song, and each id of the pool that is passed over and why.

## Edge cases

| Case                                                                | What happens                                                                                                                                               |
| ------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| A pool song is free or already owned                                | Passed over, no turn spent                                                                                                                                 |
| An id the catalog does not have, or spelt in another case           | Passed over; the list is tried to its end                                                                                                                  |
| The song already sits in the suggestions                            | It moves to the top; the column keeps its five cards                                                                                                       |
| Counting or unlocking takes over 3 s (`SONG_REWARD_PREPARE_CAP_MS`) | The result goes on to its paywalls. A song unlocked late is kept and counted, with no popup                                                                |
| The player leaves before the progress shows (`dropPreparing`)       | Nothing shows. Nothing is spent unless the unlock had begun; then the song is kept and counted                                                             |
| Weak network                                                        | The cover and the MIDI are fetched from the moment the song is unlocked (`preload`, best effort); the popup draws an empty cover box until the art arrives |

## Analytics

`reward_song_show` and `reward_song_click` (`action`: `play_now` / `later`), both with `variant`, `song_id`, `song_name`, `reward_index` (which song of the day). User property `reward_song_variant`. A song opened from the popup is a `recommended_result` play at `song_order` 1.

## UI

`SongRewardHost` (root layout) draws `SongRewardProgress` and `SongRewardPopup`. Both are **placeholders**: they take display-ready values, so the design replaces only their drawing. The time the bar stays up is `SONG_REWARD_PROGRESS_MS` in `flow-store.ts`.

***

<small>Source: [`docs/features/song-reward.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/features/song-reward.md)</small>
