-- =====================================================================
-- AI Front Desk Platform — Migration 010
--
-- Adds tenant_stt_vocabulary — per-tenant custom terms fed to Deepgram's
-- keywords_boosting (transcription_engine_config.keywords_boosting on
-- Telnyx's transcription_start command) so brand names, part numbers,
-- and staff names that Deepgram mis-hears can be boosted per tenant.
--
-- A proper child table rather than a tenants.<column> JSON blob (unlike
-- business_hours/sms_templates) because the access pattern is different:
-- those are small, fixed-shape config edited rarely as a whole document.
-- This is an open-ended list — up to ~100 rows per tenant expected —
-- edited incrementally, one term at a time ("the AI keeps mishearing
-- this part number, add it"), which maps onto normal row inserts/
-- updates/deletes far better than read-modify-write-the-whole-JSON-blob.
--
-- Only consumed by the Deepgram engine path (see telnyx.php's
-- call.answered handler) — if a tenant is ever moved to a different
-- transcription engine, this table is simply not read; nothing else
-- needs to change.
-- =====================================================================


CREATE TABLE tenant_stt_vocabulary (
    id          BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    tenant_id   BIGINT UNSIGNED NOT NULL,
    term        VARCHAR(191)    NOT NULL COMMENT 'The word/phrase to boost, exactly as Deepgram should recognize it, e.g. "Lennox" or "Dr. Okafor".',
    weight      TINYINT UNSIGNED NOT NULL DEFAULT 2 COMMENT 'Deepgram keywords_boosting intensifier. No documented hard cap — start modest (2-3) and reserve higher values (5+) for terms actually observed being mis-transcribed; over-boosting risks the model favoring this term even when the caller said something merely similar-sounding.',
    created_at  DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at  DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uq_tenant_vocab_term (tenant_id, term),
    KEY idx_tenant_vocab_tenant (tenant_id),
    CONSTRAINT fk_tenant_vocab_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
