-- ============================================================================================
-- PRODUCTION MIGRATION — Proposal/Invoice document templates
-- ============================================================================================
-- Adds a reusable, rich-text (TipTap-authored) template per document type (PROPOSAL/INVOICE),
-- with {{mergeField}} tokens substituted at generation time (see DocumentGenerationService).
-- No existing table/column touched — purely additive.
-- Idempotent — safe to re-run.
-- Deploy: mysql -u root -p db_abos_v0.1 < db/dim_document_templates.sql
-- ============================================================================================

USE db_abos_v0.1;

CREATE TABLE IF NOT EXISTS dim_document_templates (
    id          BIGINT AUTO_INCREMENT PRIMARY KEY,
    tenant_id   INT NOT NULL,
    business_id INT NULL,
    type        VARCHAR(20) NOT NULL,
    name        VARCHAR(255) NOT NULL,
    is_default  TINYINT(1) NOT NULL DEFAULT 0,
    body_html   LONGTEXT NULL,
    created_at  TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6),
    created_by  INT NULL,
    updated_at  TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    updated_by  INT NULL,
    is_deleted  TINYINT(1) NOT NULL DEFAULT 0,
    INDEX idx_doc_templates_tenant_type (tenant_id, type),
    INDEX idx_doc_templates_tenant (tenant_id)
);
