-- =============================================================================
-- larcare_command_center.sql - the SamariCare Command Center dashboard in the LarCare staff portal.
--  1. dim_web_sites gets dashboard_password_hash: the Command Center has its own password, separate from the Portal's.
--     It is set in ABOS > Website > Settings (Command Center password). With no password nobody can open the dashboard.
--  2. Two collections hold the weekly figures, typed so they are edited in ABOS > Website > Content:
--       command-center-weeks    one entry per week and client type (clients, hours, missed visits, revenue, cash ...)
--       command-center-company  one entry per week for the whole company (leads, training, quality)
--     They are NOT served by the open public collection endpoints; only the Command Center endpoints, which need the
--     Command Center access token, return them. No figures are loaded: the dashboard shows what staff enter.
-- Only the LarCare site is touched. Safe to run again.
-- =============================================================================
SET SESSION lock_wait_timeout = 15;
SET @col_exists = (
    SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'dim_web_sites' AND COLUMN_NAME = 'dashboard_password_hash'
);
SET @sql = IF(@col_exists = 0,
    'ALTER TABLE dim_web_sites ADD COLUMN dashboard_password_hash VARCHAR(100) NULL',
    'SELECT 1');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

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-weeks', 'Command Center: weekly figures by client type', 'Weekly figure', 'collection', NULL, 0, 0, 30, 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-weeks');
SET @c_weeks := (SELECT id FROM dim_web_collections WHERE site_id = @site_id AND collection_key = 'command-center-weeks' 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_weeks, 'weekEnding', 'Week ending (the Friday)', 'DATE', 1, 1, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks 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_weeks, 'clientType', 'Client type', 'SELECT', 1, 2, '{"options": ["Veterans (VA)", "Private pay", "Medicaid", "Recover at Home"]}', NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks 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_weeks, 'activeClients', 'Active clients at the end of the week', 'NUMBER', 0, 3, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'activeClients');
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_weeks, 'newStarts', 'New starts this week', 'NUMBER', 0, 4, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'newStarts');
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_weeks, 'exits', 'Exits this week', 'NUMBER', 0, 5, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'exits');
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_weeks, 'authorizedHours', 'Authorized hours', 'NUMBER', 0, 6, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'authorizedHours');
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_weeks, 'deliveredHours', 'Delivered hours', 'NUMBER', 0, 7, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'deliveredHours');
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_weeks, 'missedAgencyHours', 'Missed hours: agency (missed visit, caregiver call-out)', 'NUMBER', 0, 8, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'missedAgencyHours');
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_weeks, 'missedHospitalHours', 'Missed hours: client in hospital', 'NUMBER', 0, 9, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'missedHospitalHours');
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_weeks, 'missedVacationHours', 'Missed hours: client on vacation', 'NUMBER', 0, 10, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'missedVacationHours');
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_weeks, 'missedCancelledHours', 'Missed hours: cancelled or refused care', 'NUMBER', 0, 11, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'missedCancelledHours');
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_weeks, 'clockedVisits', 'Clocked-in visits', 'NUMBER', 0, 12, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'clockedVisits');
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_weeks, 'visitsMissingDocs', 'Visits missing a note or task sheet', 'NUMBER', 0, 13, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'visitsMissingDocs');
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_weeks, 'missingTaskSheets', 'Of those, visits missing a task sheet', 'NUMBER', 0, 14, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'missingTaskSheets');
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_weeks, 'authsEnding30', 'Authorizations ending within 30 days', 'NUMBER', 0, 15, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'authsEnding30');
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_weeks, 'initialAssessments', 'Initial assessments due', 'NUMBER', 0, 16, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'initialAssessments');
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_weeks, 'reassessments', 'Reassessments / revisits due', 'NUMBER', 0, 17, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'reassessments');
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_weeks, 'expectedRevenue', 'Expected revenue (delivered hours x rates), USD', 'NUMBER', 0, 18, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'expectedRevenue');
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_weeks, 'receivedRevenue', 'Received (QuickBooks deposits for the same work), USD', 'NUMBER', 0, 19, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'receivedRevenue');
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_weeks, 'cashCollected', 'Cash collected this week, USD', 'NUMBER', 0, 20, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'cashCollected');
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_weeks, 'next30Revenue', 'Expected revenue over the next 30 days, USD', 'NUMBER', 0, 21, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'next30Revenue');
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_weeks, 'evvExceptions', 'EVV exceptions (Medicaid only)', 'NUMBER', 0, 22, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_weeks IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_weeks AND field_key = 'evvExceptions');

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-company', 'Command Center: company weekly figures', 'Company weekly figure', 'collection', NULL, 0, 0, 31, 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-company');
SET @c_company := (SELECT id FROM dim_web_collections WHERE site_id = @site_id AND collection_key = 'command-center-company' 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_company, 'weekEnding', 'Week ending (the Friday)', 'DATE', 1, 1, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company 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_company, 'leadsOnline', 'New leads: online / intake form', 'NUMBER', 0, 2, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'leadsOnline');
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_company, 'leadsPhone', 'New leads: phone calls', 'NUMBER', 0, 3, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'leadsPhone');
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_company, 'leadsReferral', 'New leads: caseworker / VA referral', 'NUMBER', 0, 4, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'leadsReferral');
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_company, 'caregiversTotal', 'Caregivers (total)', 'NUMBER', 0, 5, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'caregiversTotal');
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_company, 'trainingOverdue', 'Caregivers overdue on training', 'NUMBER', 0, 6, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'trainingOverdue');
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_company, 'trainingCountdown', 'Caregivers in the 90-day countdown', 'NUMBER', 0, 7, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'trainingCountdown');
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_company, 'trainingMismatch', 'Caregivers where CareAcademy and ERSP do not match', 'NUMBER', 0, 8, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'trainingMismatch');
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_company, 'openCorrectiveActions', 'Open corrective actions', 'NUMBER', 0, 9, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'openCorrectiveActions');
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_company, 'auditFindings', 'Open audit findings', 'NUMBER', 0, 10, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'auditFindings');
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_company, 'complaints', 'Complaints this week', 'NUMBER', 0, 11, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'complaints');
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_company, 'incidents', 'Incidents this week', 'NUMBER', 0, 12, NULL, NULL, NULL, 0 FROM DUAL
WHERE @c_company IS NOT NULL AND NOT EXISTS (SELECT 1 FROM dim_web_collection_fields WHERE collection_id = @c_company AND field_key = 'incidents');
