-- =============================================================================
-- larcare_command_center_items.sql - the per-client lines behind the Command Center widgets.
-- One more collection on the LarCare site, edited in ABOS > Website > Content > Collections:
--     command-center-items   one entry per line: who, what, how much, how urgent, for a given week
-- The widget drawers (missed hours by client, visits to follow up, authorizations ending, assessments due, caregiver
-- training, EVV exceptions, audit findings) list these lines. Like the other Command Center collections it is NOT served by
-- the open public endpoints, only with the Command Center access token. Nothing is loaded; staff enter the lines.
-- Safe to run again.
-- =============================================================================
SET SESSION lock_wait_timeout = 15;
SET @site_id := (SELECT id FROM dim_web_sites WHERE public_site_key = 'pSlfeq9OCm5MvZyoFguIK7jy0uz2aKShJkxZxTSWXwzPOpYJ' AND is_deleted = 0 LIMIT 1);
SET @tenant_id := (SELECT tenant_id FROM dim_web_sites WHERE id = @site_id LIMIT 1);
SET @business_id := (SELECT business_id FROM dim_web_sites WHERE id = @site_id LIMIT 1);


INSERT INTO dim_web_collections (tenant_id, business_id, site_id, collection_key, label, label_singular, kind, route_pattern, has_seo_fields, is_system, sort_order, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, 'command-center-items', 'Command Center: per-client lines', 'Per-client line', 'collection', NULL, 0, 0, 32, NULL, NULL, 0 FROM DUAL
WHERE @site_id IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collections WHERE site_id = @site_id AND collection_key = 'command-center-items');
SET @c_items := (SELECT id FROM dim_web_collections WHERE site_id = @site_id AND collection_key = 'command-center-items' LIMIT 1);
INSERT INTO dim_web_collection_fields (tenant_id, business_id, site_id, collection_id, field_key, label, field_type, is_required, sort_order, config_json, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, @c_items, 'weekEnding', 'Week ending (the Friday)', 'DATE', 1, 1, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_items IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_items AND field_key = 'weekEnding');
INSERT INTO dim_web_collection_fields (tenant_id, business_id, site_id, collection_id, field_key, label, field_type, is_required, sort_order, config_json, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, @c_items, 'list', 'Which widget list is this line for', 'SELECT', 1, 2, '{"options": ["Missed hours", "Visit follow-up", "Authorization ending", "Assessment due", "Training", "EVV exception", "Audit finding"]}', NULL, NULL, 0 FROM DUAL
WHERE @c_items IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_items AND field_key = 'list');
INSERT INTO dim_web_collection_fields (tenant_id, business_id, site_id, collection_id, field_key, label, field_type, is_required, sort_order, config_json, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, @c_items, 'subject', 'Client or caregiver (use a code or initials, never a full name)', 'TEXT', 1, 3, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_items IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_items AND field_key = 'subject');
INSERT INTO dim_web_collection_fields (tenant_id, business_id, site_id, collection_id, field_key, label, field_type, is_required, sort_order, config_json, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, @c_items, 'clientType', 'Client type (Whole company for caregiver and audit lines)', 'SELECT', 0, 4, '{"options": ["Veterans (VA)", "Private pay", "Medicaid", "Recover at Home", "Whole company"]}', NULL, NULL, 0 FROM DUAL
WHERE @c_items IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_items AND field_key = 'clientType');
INSERT INTO dim_web_collection_fields (tenant_id, business_id, site_id, collection_id, field_key, label, field_type, is_required, sort_order, config_json, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, @c_items, 'detail', 'What it is (reason, what is missing, what is due)', 'TEXT', 0, 5, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_items IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_items AND field_key = 'detail');
INSERT INTO dim_web_collection_fields (tenant_id, business_id, site_id, collection_id, field_key, label, field_type, is_required, sort_order, config_json, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, @c_items, 'value', 'Amount or timing shown on the right (6 hrs, 12 days left, Due Tue)', 'TEXT', 0, 6, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_items IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_items AND field_key = 'value');
INSERT INTO dim_web_collection_fields (tenant_id, business_id, site_id, collection_id, field_key, label, field_type, is_required, sort_order, config_json, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, @c_items, 'tone', 'How urgent', 'SELECT', 0, 7, '{"options": ["Normal", "Watch", "Red flag"]}', NULL, NULL, 0 FROM DUAL
WHERE @c_items IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_items AND field_key = 'tone');
INSERT INTO dim_web_collection_fields (tenant_id, business_id, site_id, collection_id, field_key, label, field_type, is_required, sort_order, config_json, created_by, updated_by, is_deleted)
SELECT @tenant_id, @business_id, @site_id, @c_items, 'cause', 'Missed hours only: who caused it', 'SELECT', 0, 8, '{"options": ["Agency", "Client", "Mismatch"]}', NULL, NULL, 0 FROM DUAL
WHERE @c_items IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_items AND field_key = 'cause');
