<?php

namespace Database\Seeders;

use App\Models\Product;
use Illuminate\Database\Seeder;

class FixProductImageUrlsSeeder extends Seeder
{
    public function run(): void
    {
        foreach (Product::all() as $product) {
            $img = $product->image_url ?? '';

            $needsFix = $img === ''
                || str_contains($img, 'via.placeholder.com')
                || (str_starts_with($img, '/images/') && ! is_file(public_path(ltrim($img, '/'))));

            if ($needsFix) {
                $product->image_url = 'https://picsum.photos/seed/' . md5($product->name) . '/320/240';
                $product->save();
            }
        }
    }
}
