-- Phase 3: Automate Signing feature.
--
-- `automation_mode` controls whether the CRM cascades signing
-- invitations automatically as signers complete vs. leaving the rep
-- to fire each invitation manually.
--
-- Values:
--   manual           - default; rep fires each signer's invitation by hand.
--   sequential_auto  - first signer invited; webhook handler fires the
--                      next-in-order signer on each recipient_signed event.
--   concurrent_auto  - all signers invited at once; only the broadcast
--                      completion email fires from the webhook.
--
-- Default 'manual' preserves existing behaviour. Backfill not needed.

ALTER TABLE documents
  ADD COLUMN automation_mode text NOT NULL DEFAULT 'manual';

-- Defensive constraint: only the three known enum values are
-- acceptable. Service layer also validates, but having it at the DB
-- level prevents direct-SQL bypass from creating an invalid state.
ALTER TABLE documents
  ADD CONSTRAINT documents_automation_mode_check
    CHECK (automation_mode IN ('manual', 'sequential_auto', 'concurrent_auto'));
