# Agency Portals (/agency-portals)

A landing page selling custom client portals and team dashboards to marketing agencies, as a done-for-you
service. An agency enters its website and email and sees its own portal and dashboard in its brand, in about
a minute, on the model of Particular's direct-booking test.

## Files

| File | What it is |
|---|---|
| `config/agency-portals.php` | The part you edit: name, pricing tiers, the quote (empty hides it), templates, the five sample clients, daily cap |
| `src/AgencyPortals.php` | Reading a site, the brand, picking a template, colors, leads, emails |
| `views/agency-portals/landing.php` | The landing page. Its own HTML; does not use `views/layout.php` |
| `views/agency-portals/preview.php` | The preview page (also `/agency-portals/example`) |
| `views/agency-portals/templates/*.php` | Spotlight, Midnight and Bento, each a client portal and a team dashboard. `_kit.php` holds the logo, font and count-up helpers |
| `assets/agency-portals/ap.css`, `ap.js` | Styles and behavior for the landing and preview pages |
| Routes | The "Agency Portals" block near the end of `index.php` |
| Table | `agency_portal_leads`, in `setup/db_init.php` (2026-11-19) |

## The flow

1. `POST /agency-portals/preview`: CSRF, a honeypot (`company_site`), 6 an hour per IP, then `AgencyPortals::read()`.
2. The read fetches the home page and up to two of its own stylesheets. Each request goes to a public IPv4
   address pinned with `CURLOPT_RESOLVE`, redirects are re-checked, and bodies are capped. It takes the name,
   logo, Google Font, colors (with extra weight for `--primary`, `--brand` and similar), and whether the page is
   dark. One `gpt-4.1-mini` call then picks the name, services, the kind of client served, the site's personality
   and the brand color, choosing only from lists we give it, and every answer is checked.
3. Template: a dark site gets Midnight; a bold or playful one gets Bento; anything else gets Spotlight. The
   preview opens on that template, marked "Picked for you", and the other two are a click away.
4. A host read again within `AP_CACHE_DAYS` reuses the earlier read. A site that can't be read, or anything past
   `AP_DAILY_BUILDS` fresh reads in a day, still gets a preview built from its domain name, and the page says
   part of it is a guess.
5. Both sides get an email: Tyler a summary with the preview link, and the agency its own link.
6. The preview's "Want the real one?" form (`POST /agency-portals/more/{token}`) records what they sell, how many
   clients they have and their tools, sets `status = 'wants-call'`, and emails Tyler.

Templates render at `/agency-portals/render/{token}/{template}/{portal|dashboard}` inside an iframe. Everything
taken from the agency's site is escaped with `ap_e()`. Colors are adjusted until text on them is readable: darker
for the light templates, lighter for Midnight.

## Changing things

- Prices, quote, name: `config/agency-portals.php`.
- A new sample client: add it to `AP_SAMPLE_CLIENTS`, and add its key to the `client_kind` description in
  `AgencyPortals::ask()`.
- A new template: add it to `AP_TEMPLATES`, add `<key>-portal.php` and `<key>-dashboard.php`, and decide in
  `pickTemplate()` when it is the one picked.
- Leads are in the `agency_portal_leads` table. There is no admin screen; the emails are the inbox.
