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

use App\Models\Product;
use App\Models\ProductSize;
use Illuminate\Support\Facades\DB;

echo 'Products: '.Product::count().PHP_EOL;
echo 'ProductSizes (sizes_db): '.ProductSize::on('sizes_db')->count().PHP_EOL;
$existing = Product::pluck('id')->toArray();
$orphan = DB::connection('sizes_db')->table('product_sizes')->whereNotIn('product_id', $existing)->count();
echo 'OrphanSizes: '.$orphan.PHP_EOL;

// Show some orphan ids if any
if ($orphan > 0) {
    $rows = DB::connection('sizes_db')->table('product_sizes')->whereNotIn('product_id', $existing)->limit(20)->get();
    foreach ($rows as $r) {
        echo "Orphan row: id={$r->id}, product_id={$r->product_id}, size={$r->size}, stock={$r->stock}\n";
    }
}
