# Site structure (Prompt 5A)

CMS-managed structure behind the public homepage and pages. All writes are audited
(`site_content_created|updated|deleted`, `site_settings_updated`,
`story_correction_added|removed`), flush their Redis tags and queue a Next.js
revalidation (see [public-api.md](public-api.md)).

## Tables

| Table | Notes |
| --- | --- |
| `menus`, `menu_items` | nested items (`parent_id`), types `category` (category_id), `page` (page_slug), `url`, `module` (module_key + url; hidden while the module is off) |
| `layouts`, `layout_sections` | homepage builder; layout `is_default` or scheduled (`starts_at`/`ends_at`); section `type`, `settings` JSON, `sort`, schedule |
| `breaking_items` | ticker entries, `level` breaking/alert/live, `starts_at`, `expires_at` (auto-expire) |
| `pages` | translatable `title`, `summary`, `body_html` (purified, story allow-list), `seo`, `template` |
| `team_members` | Our Team; optional `user_id` → author page; `group` leadership/editorial/reporters/district/management/technical |
| `site_settings` | `key`, `group`, JSON `value`, `is_public` |
| `redirects` | `from_path` (normalised, lower-case) + `from_hash` (sha256, unique), `to_path`, `status_code`, `hits` |
| `story_corrections` | correction / clarification / update notes shown on the public story |

## Section settings (homepage builder)

Content sections fill `settings.limit` slots: `settings.pinned` (story uids) first, then
`settings.rule`:

```json
{"limit": 6, "pinned": ["01J…"], "rule": {"source": "latest|featured|trending|most_read", "categories": ["politics"], "content_type": "video_story", "tag": "flood", "topic": "monsoon-2083", "period": "day"}}
```

Stories shown higher on the page are not repeated below. `latest_news` uses `settings.tabs`
(`[{key, label, categories}]`); `news_across_nepal` uses `days` and `per_province`;
`header` / `footer` name their menus; `custom_html` stores purified `settings.html`;
`settings.module` overrides the module that gates a section (defaults: watch_nepal → video,
fm_radio → radio, explore → explore, events → events, newsletter_cta → newsletter,
ad_slot → ads, news_across_nepal → regions).

## CMS endpoints (`/api/v1/cms/site`)

| Endpoints | Permission |
| --- | --- |
| `GET/POST menus`, `PATCH/DELETE menus/{menu}`, `PUT menus/{menu}/items` (whole tree, 3 levels) | `site.menus.manage` |
| `GET/POST layouts`, `GET/PATCH/DELETE layouts/{layout}`, `PUT layouts/{layout}/sections`, `GET layouts/{layout}/preview` | `site.homepage.manage` |
| `GET/POST pages`, `PATCH/DELETE pages/{page}` | `site.pages.manage` |
| `GET/POST team`, `PATCH/DELETE team/{member}` | `site.team.manage` |
| `GET/POST breaking`, `PATCH/DELETE breaking/{item}` | `site.breaking.manage` |
| `GET settings`, `PATCH settings` (`values` map + `reason`) | `site.settings.manage` † (2FA in the last 15 min) |
| `GET/POST redirects`, `PATCH/DELETE redirects/{redirect}` | `site.redirects.manage` |
| `POST stories/{story}/corrections`, `DELETE stories/{story}/corrections/{correction}` (`/api/v1/cms`) | `corrections.manage` (+ story scope) |

`GET settings` marks values still holding `__CONFIRM_WITH_CLIENT__` with `needs_confirmation`.

## Services, repositories, cache

- `SiteService` (menus, pages, team, breaking, settings, corrections), `HomepageService`
  (layouts + section resolution), `RedirectService`, `PublicContentService`,
  `SearchService`, `FeedService`, `RevalidationService`.
- Cached repositories: `MenuRepository` (`site:menus`), `LayoutRepository` (`site:layouts`,
  60 s so schedules switch on time), `SettingsRepository` (`site:settings`),
  `PublicStoryRepository` (`stories`, `public`, 30–60 s).
- `GET /public/home` is cached with `Cache::flexible` (30 s fresh, 2 min stale) under the
  tags above plus `modules` (switching a module rebuilds it).
- Views: `ViewCounter` (Redis DB 5): `views:h:{YmdH}`, `views:d:{Ymd}` sorted sets,
  `views:seen:{story}:{sha256(visitor)}` (30 min de-dupe).

## Seeders (in `ReferenceDataSeeder`, idempotent, never overwrite CMS edits)

| Seeder | Content |
| --- | --- |
| `MenusSeeder` (`data/menus.php`) | `header` (Home, News, Watch & Listen, Business, Discover, More), old-site `main` and `more` (थप) menus from Appendix E mapped to categories, `footer-about/explore/services/policies` |
| `HomepageLayoutSeeder` | the approved design's 14 sections (SRS §7.1) |
| `PagesSeeder` | About, Our Team, Contact, Advertise, Careers, Privacy, Terms, Editorial Policy, Corrections Policy, Grievance, Send a Tip — text marked `__CONFIRM_WITH_CLIENT__` |
| `SiteSettingsSeeder` | site, contact, social, legal (registration numbers, Press Council), radio (104.6 MHz, stream URL), weather — client values are `__CONFIRM_WITH_CLIENT__` |
| `RedirectsSeeder` | `/team.php`, `/mero_palika.php` |
| `CategoriesSeeder` (+5 rows) | old-site categories with no design equivalent: crime-security, our-municipality, main-news, strange-world, profile |

## Settings (env)

`WEB_FRONTEND_URL`, `WEB_*_PATH`, `PUBLIC_S_MAXAGE`, `NEXT_REVALIDATE_URL`,
`NEXT_REVALIDATE_SECRET`, `CDN_PURGE_DRIVER`, `CLOUDFLARE_ZONE_ID`, `CLOUDFLARE_API_TOKEN`,
`SCOUT_DRIVER`, `OPENSEARCH_*` (see [search.md](search.md)).
