<?php

namespace Database\Seeders;

use App\Models\OrderManagement\BillingDesk;
use App\Models\ProductManagement\Product;
use App\Models\StockManagement\Stock;
use App\Models\StockManagement\Warehouse;
use App\Models\UserManagement\Admin;
use Database\Factories\StockFactory;
use Illuminate\Database\Seeder;;

class StockSeeder extends Seeder{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run(){

//      Product::factory(2)->has(
//                  Stock::factory(rand(1,6),['warehouse_id']) ,"stocks"
//              )
//              ->create(); // Creer 5 Entrepots => Magasins

      $products =   Product::all();

      if(filled($products)){
          $point_of_sales = Warehouse::pointofSales()->get();
          $stores = Warehouse::stores()->get();
          //$warehouses = array_merge($point_of_sales,$stores);

          foreach ($products as $product) {

             if (filled($stores)){
                 foreach ($stores as $store) {
                     for ($i = 1 ; $i <= rand(1,6); $i++) {
                         $expired_at = fake()->dateTimeBetween(date("Y-m-d",strtotime("+1years")),date("Y-m-d",strtotime("+". rand(2,6)." years")));
                         $product->stocks()
                             ->create( [
                                 'lot_id'        => uniqid(),
                                 'qty'           => rand(50,500),
                                 'expired_at'    => format_date($expired_at ,"Y-m-d"),
                                 'warehouse_id'    => $store->id,
                             ]);
                     }
                 }
             }

             if (filled($point_of_sales)){
                 foreach ( $point_of_sales as $pos) {
                     for ($i = 1 ; $i <= rand(1,6); $i++) {
                         $expired_at = fake()->dateTimeBetween(date("Y-m-d",strtotime("+1years")),date("Y-m-d",strtotime("+". rand(2,6)." years")));
                         $product->stocks()
                                 ->create( [
                                     'lot_id'        => uniqid(),
                                     'qty'           => rand(50,500),
                                     'expired_at'    => format_date($expired_at ,"Y-m-d"),
                                     'warehouse_id'    => $pos->id,
                                 ]);
                     }
                 }
             }


          }
      }
        echo __("Products Stock created \n");
    }
}
