-- settings-contracts (wave-13, P165)
-- Extends contracts table with signing_order column.
-- Extends tenant_settings with all contract-configuration columns.
-- Base tables owned by contracts-esignature and foundation-auth-rbac respectively.

-- contracts: new column only (expiry_date/effective_date/renewed_from_id/amended_from_id
-- already exist from contract-renewal-amendment; do NOT redefine them).
ALTER TABLE contracts
  ADD COLUMN IF NOT EXISTS signing_order TEXT NOT NULL DEFAULT 'parallel'
    CHECK (signing_order IN ('parallel', 'ordered'));
--> statement-breakpoint
-- tenant_settings: contract configuration columns
ALTER TABLE tenant_settings
  ADD COLUMN IF NOT EXISTS contract_default_template_id UUID,
  ADD COLUMN IF NOT EXISTS contract_default_expiry_days INTEGER NOT NULL DEFAULT 365
    CHECK (contract_default_expiry_days >= 0),
  ADD COLUMN IF NOT EXISTS contract_default_signing_order TEXT NOT NULL DEFAULT 'parallel'
    CHECK (contract_default_signing_order IN ('parallel', 'ordered')),
  ADD COLUMN IF NOT EXISTS contract_require_email_verification BOOLEAN NOT NULL DEFAULT true,
  ADD COLUMN IF NOT EXISTS contract_reminder_first_days INTEGER NOT NULL DEFAULT 3
    CHECK (contract_reminder_first_days >= 0),
  ADD COLUMN IF NOT EXISTS contract_reminder_followup_days INTEGER NOT NULL DEFAULT 7
    CHECK (contract_reminder_followup_days >= 0),
  ADD COLUMN IF NOT EXISTS contract_reminder_max_count INTEGER NOT NULL DEFAULT 3
    CHECK (contract_reminder_max_count >= 0),
  ADD COLUMN IF NOT EXISTS contract_renewal_remind_30d BOOLEAN NOT NULL DEFAULT true,
  ADD COLUMN IF NOT EXISTS contract_renewal_remind_7d BOOLEAN NOT NULL DEFAULT true,
  ADD COLUMN IF NOT EXISTS contract_renewal_remind_1d BOOLEAN NOT NULL DEFAULT false,
  ADD COLUMN IF NOT EXISTS contract_renewal_notify_email BOOLEAN NOT NULL DEFAULT true,
  ADD COLUMN IF NOT EXISTS contract_renewal_notify_in_app BOOLEAN NOT NULL DEFAULT true;
