<?php

namespace Database\Factories;

use App\Models\OrderManagement\BillingDesk;
use App\Models\ProductManagement\Product;
use App\Models\StockManagement\Stock;
use App\Models\StockManagement\Warehouse;
use App\Models\StockManagement\WareHouseType;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
 */
class StockFactory extends Factory{
    protected $model = Stock::class;

    public function definition(){
        $product = Product::all()->random();
        $expired_at = fake()->dateTimeBetween(date("Y-m-d",strtotime("+1years")),date("Y-m-d",strtotime("+". rand(2,6)." years")));
        return [
            'lot_id'        => uniqid(),
            'product_id'    => $product->id ?? null,
            'qty'           => rand(10,500),
            'expired_at'    => format_date($expired_at ,"Y-m-d"),
        ];
    }


    public function isStore(): Factory{
        return $this->state(function (array $attributes) {
            return [
                'warehouse_type_code' => WareHouseType::STORE,
                'is_bad_goods' => rand(1,0),
            ];
        });
    }

    public function isPointOfSales(): Factory{
        return $this->state(function (array $attributes) {
            $stores = Warehouse::stores()->get();

            $store = filled($stores) ? $stores->random() : Warehouse::factory()->isStore()->create();
            return [
                'warehouse_type_code' => WareHouseType::POINT_OF_SALE,
                'store_id' => $store->getKey(),
            ];
        });
    }
}
