-- 产品收集字段(回访中心信息字段.docx 吸收)
--   1) Patient: tags + primary_contact_type
--   2) PlanReason: lifecycle / source / source_actor_id / campaign_id / closed_reason / closed_at + index
--   3) PlanExecution: invalid_reason

-- 1) Patient
ALTER TABLE "patients"
  ADD COLUMN IF NOT EXISTS "tags" TEXT[] NOT NULL DEFAULT '{}',
  ADD COLUMN IF NOT EXISTS "primary_contact_type" TEXT;

-- 2) PlanReason
ALTER TABLE "plan_reasons"
  ADD COLUMN IF NOT EXISTS "lifecycle" TEXT NOT NULL DEFAULT 'one_shot',
  ADD COLUMN IF NOT EXISTS "source" TEXT NOT NULL DEFAULT 'algorithm',
  ADD COLUMN IF NOT EXISTS "source_actor_id" TEXT,
  ADD COLUMN IF NOT EXISTS "campaign_id" TEXT,
  ADD COLUMN IF NOT EXISTS "closed_reason" TEXT,
  ADD COLUMN IF NOT EXISTS "closed_at" TIMESTAMPTZ(3);

CREATE INDEX IF NOT EXISTS "plan_reasons_campaign_id_idx" ON "plan_reasons" ("campaign_id");

-- 3) PlanExecution
ALTER TABLE "plan_executions"
  ADD COLUMN IF NOT EXISTS "invalid_reason" TEXT;
