-- 0061_supplemental_form_tokens.sql
-- ----------------------------------------------------------------------------
-- Pre-EOI supplemental info form tokens. One row per public form link the
-- CRM emails to a client. Token-keyed lookups + soft expiry + one-shot
-- consumption.

CREATE TABLE IF NOT EXISTS supplemental_form_tokens (
  id text PRIMARY KEY DEFAULT gen_random_uuid()::text,
  port_id text NOT NULL REFERENCES ports(id),
  interest_id text NOT NULL REFERENCES interests(id) ON DELETE CASCADE,
  client_id text NOT NULL REFERENCES clients(id) ON DELETE CASCADE,
  token text NOT NULL UNIQUE,
  created_at timestamptz NOT NULL DEFAULT now(),
  expires_at timestamptz NOT NULL,
  consumed_at timestamptz,
  issued_by text
);

CREATE INDEX IF NOT EXISTS idx_supplemental_tokens_interest
  ON supplemental_form_tokens (interest_id);
CREATE INDEX IF NOT EXISTS idx_supplemental_tokens_client
  ON supplemental_form_tokens (client_id);
CREATE INDEX IF NOT EXISTS idx_supplemental_tokens_port
  ON supplemental_form_tokens (port_id);
