-- Private-pay workflow follow-ups (LarCare): discovery-call calendar invites and imported Fireflies transcripts.
-- Additive and idempotent. Run after private_care_cases.sql and private_care_settings.sql.

USE db_abos_v0.1;

ALTER TABLE fact_shiftcare_referrals
  ADD COLUMN IF NOT EXISTS discovery_link    VARCHAR(500) NULL,
  ADD COLUMN IF NOT EXISTS discovery_minutes INT          NOT NULL DEFAULT 30,
  ADD COLUMN IF NOT EXISTS invite_seq        INT          NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS invite_sent_at    TIMESTAMP(6) NULL;

-- A discovery-call transcript brought in from Fireflies, kept with the case it belongs to.
CREATE TABLE IF NOT EXISTS fact_private_care_transcripts (
  id               BIGINT AUTO_INCREMENT PRIMARY KEY,
  case_id          BIGINT       NOT NULL,
  tenant_id        INT          NOT NULL,
  source           VARCHAR(30)  NOT NULL DEFAULT 'Fireflies',
  external_id      VARCHAR(120) NULL,
  title            VARCHAR(255) NULL,
  meeting_at       TIMESTAMP    NULL,
  duration_minutes DECIMAL(8,1) NULL,
  summary          TEXT         NULL,
  segments_json    LONGTEXT     NULL,
  imported_by      INT          NULL,
  imported_at      TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY uq_pc_transcript_case (case_id),
  KEY idx_pc_transcript_tenant (tenant_id)
);

-- The tenant's Fireflies API key, saved encrypted from Website > Workflows (never returned to the browser).
ALTER TABLE dim_private_care_settings
  ADD COLUMN IF NOT EXISTS fireflies_api_key TEXT NULL;
