

CREATE TABLE IF NOT EXISTS cvm_sync_logs (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    started_at DATETIME NOT NULL,
    finished_at DATETIME NULL,
    status VARCHAR(30) NOT NULL,
    message TEXT NULL,
    total_found INT NOT NULL DEFAULT 0,
    total_inserted INT NOT NULL DEFAULT 0,
    total_updated INT NOT NULL DEFAULT 0,
    raw_html MEDIUMTEXT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS cvm_documents (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    row_hash CHAR(64) NOT NULL UNIQUE,
    codigo_cvm VARCHAR(20) NOT NULL,
    empresa VARCHAR(255) NULL,
    categoria VARCHAR(255) NULL,
    tipo VARCHAR(255) NULL,
    especie VARCHAR(255) NULL,
    data_referencia DATETIME NULL,
    data_entrega DATETIME NULL,
    status_documento VARCHAR(100) NULL,
    modalidade VARCHAR(255) NULL,
    assunto TEXT NULL,
    document_url TEXT NULL,
    published TINYINT(1) NOT NULL DEFAULT 0,
    featured TINYINT(1) NOT NULL DEFAULT 0,
    public_title VARCHAR(255) NULL,
    public_summary TEXT NULL,
    raw_columns_json JSON NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_published (published),
    INDEX idx_data_entrega (data_entrega),
    INDEX idx_data_referencia (data_referencia),
    INDEX idx_categoria (categoria(100)),
    INDEX idx_tipo (tipo(100))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
