CREATE TABLE customer_documents (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    lead_id INT UNSIGNED NOT NULL,
    crefaz_document_id BIGINT UNSIGNED NULL,
    document_id INT NOT NULL,
    document_name VARCHAR(150) NULL,
    storage_path VARCHAR(255) NOT NULL,
    checksum VARCHAR(128) NULL,
    mime_type VARCHAR(120) NULL,
    file_size INT UNSIGNED NULL,
    uploaded_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    KEY idx_customer_documents_lead (lead_id),
    KEY idx_customer_documents_document (document_id),
    CONSTRAINT fk_customer_documents_lead FOREIGN KEY (lead_id) REFERENCES leads (id) ON DELETE CASCADE,
    CONSTRAINT fk_customer_documents_crefaz FOREIGN KEY (crefaz_document_id) REFERENCES crefaz_documents (id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
