-- Links between a CRM record (lead, deal, contact, client, invoice) and Inventory items, Projects or Tasks.
-- One CRM record can be linked to many items, and an item to many CRM records. Idempotent: safe to re-run.
CREATE TABLE IF NOT EXISTS fact_record_links (
    id           BIGINT AUTO_INCREMENT PRIMARY KEY,
    tenant_id    INT NOT NULL,
    crm_type     VARCHAR(30) NOT NULL,
    crm_id       BIGINT NOT NULL,
    target_type  VARCHAR(30) NOT NULL,
    target_id    BIGINT NOT NULL,
    created_at   DATETIME(6) NULL,
    created_by   INT NULL,
    is_deleted   TINYINT(1) NOT NULL DEFAULT 0,
    KEY idx_record_links_crm (tenant_id, crm_type, crm_id, is_deleted),
    KEY idx_record_links_target (tenant_id, target_type, target_id, is_deleted)
);
