<?php

namespace Database\Seeders;

use App\Models\ProductManagement\Category;
use App\Models\ProductManagement\Product;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class ProductsTableSeeder extends Seeder{

    public function run(){

        //Cree 100 produits
       // Product::factory(100)->create();


        $products = Product::whereDoesntHave("media")->get();
        $categories = Category::whereDoesntHave("media")->get();

        if(filled($products)){
            foreach ($products as $product) {
                $images =  Storage::disk("assets")->allFiles("frontend/assets/products");


                if(filled($images) && blank($product->files)){
                    $images =  collect($images)->filter( fn ($url) => !Str::contains($url,['-','alt',"detail"]));
                    $images = $images->map( fn ($url) => url($url))->toArray();
//                    $images =  fake()->randomElements($images,rand(1,3));
                    $images =  fake()->randomElements($images);
                    $product->uploadFiles($images);
                }
            }
        }

        if(filled($categories)){
            foreach ($categories as $category) {
                $images =  Storage::disk("assets")->allFiles("frontend/assets/categories");

                if(filled($images) && blank($category->files)){
                    $images =  collect($images)->filter( fn ($url) => !Str::contains($url,['alt',"detail"]));
                    $images = $images->map( fn ($url) => url($url))->toArray();
                    $images =  fake()->randomElements($images);
                    $category->uploadFiles($images);
                }
            }
        }
//
    }
}
