-- =============================================================================
-- fix_mystateconnews_notice_autonumber.sql — automatic notice numbers and
-- publication time-stamps for mystateCONnews.
--
-- 1. Marks the Notices collection's "noticeNumber" field as auto-numbered
--    (config_json {"autoNumber": {"digits": 5}}). abos-api's WebEntryService
--    then fills it with the next number (00001, 00002, ...) when a notice is
--    approved (published) in Aggie > Website with the number left blank. A
--    number typed by hand is kept.
-- 2. Gives notice 00001 its publication time-stamp: it was published by
--    publish_mystateconnews_notice_00001.sql (12 Nov 2026, 13:09:17 UTC),
--    which set its status but not published_at.
--
-- Only touches rows still in the expected state. Idempotent.
-- =============================================================================

SET NAMES utf8mb4;
SET @site_id := (SELECT id FROM dim_web_sites WHERE public_site_key = 'YPIYmVgmODxVTxgcSNwb1yAKdCtFe1M9kGT5YnTxEkL3xxs9' LIMIT 1);
SET @notices := (SELECT id FROM dim_web_collections WHERE site_id = @site_id AND collection_key = 'notices' LIMIT 1);

UPDATE dim_web_collection_fields
SET config_json = '{"autoNumber":{"digits":5}}',
    label = 'Notice number (filled in automatically when the notice is published)'
WHERE collection_id = @notices AND field_key = 'noticeNumber'
  AND (config_json IS NULL OR config_json NOT LIKE '%autoNumber%');

UPDATE fact_web_entries
SET published_at = '2026-11-12 13:09:17'
WHERE site_id = @site_id AND collection_id = @notices
  AND slug = '00001-larcare-home-health-aide-services'
  AND status = 'published' AND published_at IS NULL;
