CREATE TABLE IF NOT EXISTS platform_vehicle_source_provider (
    vin VARCHAR(64) NOT NULL,
    protocol VARCHAR(32) NOT NULL,
    source_key VARCHAR(256) NOT NULL,
    source_ref CHAR(64) NOT NULL,
    provider_name VARCHAR(128) NOT NULL,
    updated_by VARCHAR(128) NOT NULL,
    updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
    PRIMARY KEY (vin, protocol, source_key),
    UNIQUE KEY uk_vehicle_source_provider_ref (source_ref),
    INDEX idx_vehicle_source_provider_name (provider_name),
    INDEX idx_vehicle_source_provider_updated (updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS platform_vehicle_source_provider_audit (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    vin VARCHAR(64) NOT NULL,
    version INT NOT NULL,
    protocol VARCHAR(32) NOT NULL,
    source_ref CHAR(64) NOT NULL,
    source_key VARCHAR(256) NOT NULL,
    old_provider VARCHAR(128) NOT NULL DEFAULT '',
    new_provider VARCHAR(128) NOT NULL DEFAULT '',
    actor VARCHAR(128) NOT NULL,
    note VARCHAR(255) NOT NULL,
    summary VARCHAR(500) NOT NULL,
    changed_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    UNIQUE KEY uk_vehicle_source_provider_audit_version (vin, version),
    INDEX idx_vehicle_source_provider_audit_time (vin, changed_at),
    INDEX idx_vehicle_source_provider_audit_ref (source_ref)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
