key: build_a_directory
recipe:
  when: "programmatic SEO \u2014 many per-city pages listing businesses in a category\
    \ (e.g. 'plumbers in {city}')"
  preferred_path: "Two concepts: CATEGORIES (top-level verticals with SEO templates)\
    \ + LISTINGS (individual businesses inside them, grouped by city). Create a category,\
    \ bulk-import listings from an IDAP dump or SpiderMaps job, and the platform auto-generates\
    \ /directory/{category}/ + /directory/{category}/{city}/ + /directory/{category}/{city}/{listing}\
    \ pages with SEO title/description rendered from your templates. The '/directory'\
    \ prefix is PER-TENANT and renamable \u2014 see url_structure.custom_prefix."
  steps:
  - directory_create_category(name='Plumbers', slug='plumbers', seo_title_template='Best
    {category} in {city} | Acme Directory', seo_description_template='Find top-rated
    {category} in {city}. Compare ratings, reviews, and contact info.')
  - "# Populate from the tenant's own IDAP corpus \u2014 ONE call, no sync pipeline:"
  - directory_import_from_idap(category_slug='plumbers', category_filter='Plumber',
    country_code='US', city='Miami', rating_min=4.0, limit=5000)
  - "# Non-IDAP source (hand-built list, third-party feed) \u2014 use bulk upsert\
    \ instead:"
  - 'directory_bulk_upsert_listings(category_slug=''plumbers'', listings=[{name, slug?,
    city, state?, phone?, website?, rating?, review_count?, data?: {hours: [...]}},
    ...])'
  - "# No publish step \u2014 listings default to status='published'. No deploy step\
    \ \u2014 pages render live."
  - "# Verify: curl /content/directory/categories/plumbers \u2192 list of cities with\
    \ listing counts"
  re_import_loop:
    why: "\U0001F534 BOTH import paths are an UPSERT KEYED ON (category, slug) and\
      \ NEITHER REMOVES ANYTHING. A business deleted from IDAP, or renamed so it now\
      \ produces a different slug, leaves its old listing PUBLISHED FOREVER \u2014\
      \ and every re-import drifts the category further from its source. Nothing errors;\
      \ the stale listing just keeps serving."
    fix: "directory_import_from_idap(..., prune=true) ARCHIVES (status='archived',\
      \ never deletes; reversible via directory_upsert_listing) every listing the\
      \ import's own result set did not produce. It is OFF BY DEFAULT \u2014 a re-import\
      \ you ran without it did not prune."
    refusals: "prune refuses rather than guess, and names which in `prune_skipped_reason`:\
      \ source_returned_no_rows (an empty result is far more often a filter that matched\
      \ nothing than a category that genuinely emptied \u2014 pruning there would\
      \ archive everything) and source_truncated_at_limit (the SELECT hit `limit`,\
      \ so 'absent from the page' is a pagination artefact, not a deletion \u2014\
      \ raise limit or narrow the filter). A refusal is an ANSWER: re-running unchanged\
      \ refuses identically."
    cadence: "Re-run the same import with prune=true whenever the source moves. There\
      \ is no scheduler \u2014 the loop is yours to drive."
    stats: "directory_refresh_stats() rebuilds the city_stats MATERIALIZED VIEW (the\
      \ city list on hubs and city pages). It does NOT recompute a category's listing_count\
      \ \u2014 that column is maintained by the write paths only. Two similar names,\
      \ two different numbers."
  seo_templates:
    placeholders: "{category}, {city}, {listing} \u2014 rendered server-side on every\
      \ directory page"
    example_title: Best {category} in {city} | Acme
    example_description: Compare {category} in {city}. Ratings, reviews, hours, directions.
    sitemap: Every category, every (category,city), and every published listing gets
      a sitemap.xml entry automatically
  url_structure:
    category_hub: "/{directory_base}/{category_slug}                         \u2192\
      \ cities list"
    city_page: "/{directory_base}/{category_slug}/{city_slug}             \u2192 listings\
      \ in that city"
    listing_page: "/{directory_base}/{category_slug}/{city_slug}/{listing_slug} \u2192\
      \ single listing detail"
    custom_prefix: "{directory_base} is 'directory' unless the tenant changed it.\
      \ Set it with template_update_config(directory_base='ls') \u2192 /ls/plumbers/manchester/acme.\
      \ REFUSED with 422 if it is malformed (^[a-z0-9][a-z0-9-]*$), a reserved namespace\
      \ (blog, docs, changelog, press, newsroom, f, lp, api), or collides with an\
      \ existing page slug or a collection route_base. \U0001F534 A RENAME IS NOT\
      \ LIVE UNTIL YOU DEPLOY THE SITE. The prefix rides _config.json into the tenant's\
      \ KV, which only a deploy rewrites, so template_update_config saves your intent\
      \ and content_deploy_site applies it. Until then the site keeps serving the\
      \ OLD prefix; the config read reports directory_base_pending=true and names\
      \ both values, and content_deploy_readiness carries a 'Directory URL Prefix'\
      \ item saying so. RENAMING IS SAFE FOR SEO: sitemap.xml follows the DEPLOYED\
      \ prefix, never the pending one, so it can never advertise a URL the site 404s;\
      \ once deployed it advertises only the new prefix, and the old /directory/...\
      \ URLs keep working as permanent 301 redirects to it. A hub page published at\
      \ slug <directory_base> renders at /{directory_base} in place of the default."
    city_slug: "LOWER(city + '-' + state), stripped of non-alphanumeric (e.g. 'Miami\
      \ Beach' + 'Florida' \u2192 'miami-beach-florida')"
    flat_alternative: 'The three paths above are the CATEGORY-first layout. A page
      with template=''dynamic_list'' and collection_type=''directory_listings'' gives
      you a FLAT one instead: every published listing on /{page_slug}, each item at
      /{page_slug}/{listing_slug}, no category or city in the URL. Both layouts can
      coexist over the same listings. Backed by GET /content/directory/listings[?category=&city=&page=&page_size=]
      and GET /content/directory/listings/{listing_slug}[?category=]. A listing slug
      is unique per CATEGORY, not per tenant, so where two categories share one the
      flat detail door returns a deterministic first match and names the category
      it chose in `category_slug`; pass ?category= to pin it.'
  listing_fields:
    required:
    - name
    common:
    - slug
    - description
    - city
    - state
    - country
    - address
    - phone
    - email
    - website
    - rating
    - review_count
    - latitude
    - longitude
    flexible: "`data` JSONB \u2014 stick hours, amenities, images, anything the SEO\
      \ template needs"
    traceability: "`source_job_id` \u2014 UUID of the SpiderIQ job that produced this\
      \ listing"
  ecosystem_integration:
    idap_flow: "One call: directory_import_from_idap(category_slug, {category_filter,\
      \ country_code, city, rating_min, limit, prune}) reads the tenant's norm_cli_*.businesses\
      \ directly \u2014 no scheduler, no sync pipeline, no export step \u2014 and\
      \ carries every declared column through. Use directory_bulk_upsert_listings\
      \ only for a NON-IDAP source (a hand-built list, a third-party feed); there,\
      \ set source_job_id so you can audit provenance."
    spidermaps_flow: "Run a SpiderMaps campaign \u2192 collect results \u2192 directory_bulk_upsert_listings(category_slug,\
      \ listings=results). For large imports, paginate at 5000 per call."
    merge_tags: "\U0001F534 NOT AVAILABLE ON DIRECTORY PAGES. Merge tags ({{ first_name\
      \ }}, {{ business_name }}, \u2026) are /lp/ LEAD-SCOPED ONLY: they are built\
      \ from a lead resolved by the /lp/{page_slug}/{identifier} routes, and on every\
      \ other route \u2014 directory pages included \u2014 they resolve to the canonical\
      \ empty shape. Directory templates read `listing.*` (the typed columns) and\
      \ `data.*` (whatever you stored in the listing's data JSONB) instead."
  anti_patterns:
  - "DO NOT create a category per city \u2014 one category spans all cities. Cities\
    \ are derived from listings."
  - "DO NOT manually manage city_slug \u2014 the materialized view computes it from\
    \ city + state."
  - "DO NOT bulk-import more than 5000 listings in one call \u2014 paginate larger\
    \ imports to avoid txn timeouts."
  - "DO NOT bypass the bulk endpoint for IDAP dumps \u2014 individual upserts work\
    \ but burn 100\xD7 the API budget."
  see:
  - directory
  - merge_tags
  - sitemap
