<?php

namespace Database\Factories;

use App\Modules\Catalog\Models\Product;
use App\Modules\Vendor\Models\Vendor;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class ProductFactory extends Factory
{
    protected $model = Product::class;

    public function definition(): array
    {
        $name = fake()->words(3, true);

        return [
            'vendor_id'          => Vendor::factory(),
            'name'               => ['en' => ucwords($name), 'ne' => ucwords($name)],
            // Str::random() rather than fake()->unique()->numerify('####'):
            // the 4-digit pool (10,000 combos) was getting exhausted partway
            // through a full test-suite run, since Faker's unique() registry
            // is shared for the life of the PHP process, not reset per test —
            // VendorPlanServiceTest's "unlimited products" test alone creates
            // enough products to hit the OverflowException.
            'slug'               => Str::slug($name) . '-' . Str::lower(Str::random(8)),
            'product_type'       => 'simple',
            'price'              => fake()->randomFloat(2, 50, 5000),
            'currency'           => 'NPR',
            'track_inventory'    => true,
            'stock_quantity'     => fake()->numberBetween(10, 100),
            'low_stock_threshold'=> 5,
            'allow_backorder'    => false,
            'status'             => 'active',
            'requires_shipping'  => true,
            'tax_exempt'         => false,
            'is_featured'        => false,
        ];
    }

    public function outOfStock(): static
    {
        return $this->state(['stock_quantity' => 0]);
    }

    public function lowStock(): static
    {
        return $this->state(['stock_quantity' => 2, 'low_stock_threshold' => 5]);
    }

    public function trackingDisabled(): static
    {
        return $this->state(['track_inventory' => false]);
    }
}
