<?php
require __DIR__ . '/vendor/autoload.php';
$app = require __DIR__ . '/bootstrap/app.php';
$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

// Check what's currently in DB for colliding slugs
$collisions = [
    'claas-webtic-offline',
    'komatsu-linkone-css-net-epc-europe',
];
$products = \DB::table('products')->whereIn('slug', $collisions)->get();
echo "Products to fix:\n";
foreach ($products as $p) {
    echo "  {$p->slug}: name='{$p->name}' sku='{$p->sku}'\n";
}

// Check the NEW slugs that can't be imported
$newSlugs = [
    'claas-webtic-offline-arabic',
    'komatsu-linkone-css-net-epc-europe',
];
$newProducts = \DB::table('products')->whereIn('slug', $newSlugs)->get();
echo "\nNew slugs that failed:\n";
foreach ($newProducts as $p) {
    echo "  {$p->slug}: name='{$p->name}' sku='{$p->sku}'\n";
}

// Log: claas-webtic-offline has wrong name "CLAAS WEBTIC OFFLINE ARABIC"
// The correct mapping: claas-webtic-offline -> should be base CLAAS WEBTIC OFFLINE
// claas-webtic-offline-arabic -> CLAAS WEBTIC OFFLINE ARABIC

// Since we can't easily fix without re-fetching, let's just update the name
\DB::table('products')->where('slug', 'claas-webtic-offline')->update([
    'name' => 'CLAAS WEBTIC OFFLINE',
    'meta_title' => 'CLAAS WEBTIC OFFLINE',
]);
echo "\nUpdated claas-webtic-offline name\n";

// Remove the 'komatsu-linkone-css-net-epc-europe' entry if it exists with wrong slug pattern
$count = \DB::table('products')->where('slug', 'komatsu-linkone-css-net-epc-europe')->delete();
if ($count) {
    echo "Deleted komatsu-linkone-css-net-epc-europe (duplicate)\n";
} else {
    echo "komatsu-linkone-css-net-epc-europe not found\n";
}
