CREATE TABLE lead_rvx_snapshots (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    lead_id INT UNSIGNED NOT NULL,
    profile_id INT UNSIGNED NULL,
    integration_id INT UNSIGNED NULL,
    status VARCHAR(30) NOT NULL,
    message VARCHAR(255) NULL,
    token_issued_at DATETIME NULL,
    credits JSON NULL,
    benefits JSON NULL,
    personal_data JSON NULL,
    contracts JSON NULL,
    margins JSON NULL,
    raw_responses JSON NULL,
    fetched_at DATETIME NOT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NULL,
    CONSTRAINT fk_rvx_snapshots_lead FOREIGN KEY (lead_id) REFERENCES leads (id) ON DELETE CASCADE,
    CONSTRAINT fk_rvx_snapshots_profile FOREIGN KEY (profile_id) REFERENCES lead_rvx_profiles (id) ON DELETE SET NULL,
    CONSTRAINT fk_rvx_snapshots_integration FOREIGN KEY (integration_id) REFERENCES api_integrations (id) ON DELETE SET NULL,
    CONSTRAINT fk_rvx_snapshots_user FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL,
    INDEX idx_rvx_snapshots_lead (lead_id),
    INDEX idx_rvx_snapshots_fetched (fetched_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
