-- Migration 036: Recorders become physical hardware, not user-created rows.
--
-- A recorder now maps 1:1 to a physical capture port: (node_id, device_index).
-- mam-api auto-provisions one row per port from each node-agent heartbeat's
-- capabilities (deltacast/blackmagic arrays). Rows are NEVER deleted by the
-- operator — they're discovered, enabled/disabled, and configured in place.
-- This removes the delete/create churn that orphaned standby sidecars and
-- caused capture-port (EADDRINUSE) collisions.
--
-- New columns:
--   label    : optional friendly name overlaid on the hardware identity
--              (e.g. "Aurora" for zampp3-dc0). NULL → UI shows node+port name.
--   enabled  : operator opt-in. false (default) = no standby sidecar, port idle.
--              true = persistent standby sidecar kept up (idle-preview), ready
--              to record. Toggled by the Enable/Disable button.
--   auto_provisioned : true when the row was created by heartbeat discovery
--              (vs a legacy manually-created recorder). Informational.
--
-- Identity:
--   UNIQUE(node_id, device_index) is the structural guarantee that two
--   recorders can never share a capture port — the root-cause fix for the
--   collisions. Partial unique index (WHERE both are non-null) so any legacy
--   rows without a node/device don't violate it.

ALTER TABLE recorders
  ADD COLUMN IF NOT EXISTS label            TEXT    DEFAULT NULL,
  ADD COLUMN IF NOT EXISTS enabled          BOOLEAN NOT NULL DEFAULT false,
  ADD COLUMN IF NOT EXISTS auto_provisioned BOOLEAN NOT NULL DEFAULT false;

-- One recorder per physical port. Partial so pre-existing rows lacking a
-- node_id/device_index (e.g. network sources) are unaffected.
CREATE UNIQUE INDEX IF NOT EXISTS recorders_node_device_uniq
  ON recorders (node_id, device_index)
  WHERE node_id IS NOT NULL AND device_index IS NOT NULL;

-- Fast lookup of a node's ports during heartbeat reconciliation.
CREATE INDEX IF NOT EXISTS recorders_node_id_idx
  ON recorders (node_id);
