CREATE TABLE IF NOT EXISTS platform_vehicle_source_policy_version (
    vin VARCHAR(32) NOT NULL PRIMARY KEY,
    version INT NOT NULL DEFAULT 1,
    updated_by VARCHAR(128) NOT NULL DEFAULT 'system',
    updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS platform_vehicle_source_policy_audit (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    vin VARCHAR(32) NOT NULL,
    version INT NOT NULL,
    protocol VARCHAR(32) NOT NULL,
    source_ref CHAR(64) NOT NULL,
    source_key VARCHAR(256) NOT NULL,
    source_label VARCHAR(160) NOT NULL,
    old_enabled TINYINT(1) NOT NULL,
    new_enabled TINYINT(1) NOT NULL,
    old_priority INT NOT NULL,
    new_priority INT NOT NULL,
    old_remark VARCHAR(255) NOT NULL DEFAULT '',
    new_remark VARCHAR(255) NOT NULL DEFAULT '',
    actor VARCHAR(128) NOT NULL,
    summary VARCHAR(500) NOT NULL,
    changed_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    UNIQUE KEY uk_vehicle_source_policy_version (vin, version),
    KEY idx_vehicle_source_policy_audit_time (vin, changed_at),
    KEY idx_vehicle_source_policy_source_ref (source_ref)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
