-- =============================================================================
-- Integration toggle table
-- =============================================================================
-- Operator-managed disable list for the /status page and integration health
-- summary.  Keyed by the same identifier the integration-health service
-- emits (e.g. 's1', 'datto_rmm', 'itglue', 'msgraph', 'autotask', 'veeam',
-- 'auvik', 'addigy', 'mimecast', 'duo', 'zabbix', 'qbo', 'anthropic').
--
-- The legacy INTEGRATIONS_DISABLED env var still works (its values merge with
-- this table at read time), but DB-backed toggles take effect within the
-- 5-minute health cache without a container restart.
-- =============================================================================

CREATE TABLE IF NOT EXISTS integration_settings (
  key              TEXT PRIMARY KEY,
  disabled         BOOLEAN NOT NULL DEFAULT false,
  disabled_reason  TEXT,
  disabled_by      TEXT,                            -- session.user.email
  disabled_at      TIMESTAMPTZ,
  created_at       TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at       TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

COMMENT ON TABLE integration_settings IS
  'Per-integration operator config. Today only the disabled flag is exposed; expand as needed.';

-- Seed empty rows for known integrations so the admin UI shows everything
-- on first load even before anyone toggles anything.  The admin UI auto-
-- discovers from the integration-health response, so this seed is purely
-- a convenience.
INSERT INTO integration_settings (key, disabled) VALUES
  ('autotask',   false),
  ('datto_rmm',  false),
  ('itglue',     false),
  ('s1',         false),
  ('veeam',      false),
  ('msgraph',    false),
  ('auvik',      false),
  ('addigy',     false),
  ('mimecast',   false),
  ('duo',        false),
  ('zabbix',     false),
  ('qbo',        false),
  ('anthropic',  false)
ON CONFLICT (key) DO NOTHING;
