<?php

namespace Database\Factories;

use App\Models\Category;
use App\Models\Partner;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class ProductFactory extends Factory
{
    public function definition(): array
    {
        $products = [
            ['name' => 'Beras Pulen 5kg',       'unit' => 'karung', 'buy' => 55000,  'sell' => 63000],
            ['name' => 'Minyak Goreng 1L',       'unit' => 'botol',  'buy' => 15000,  'sell' => 18000],
            ['name' => 'Gula Pasir 1kg',         'unit' => 'kg',     'buy' => 13000,  'sell' => 16000],
            ['name' => 'Telur Ayam 1kg',         'unit' => 'kg',     'buy' => 25000,  'sell' => 28000],
            ['name' => 'Keripik Singkong Pedas', 'unit' => 'bungkus','buy' => 7000,   'sell' => 10000],
            ['name' => 'Kue Lapis',              'unit' => 'loyang', 'buy' => 20000,  'sell' => 27000],
            ['name' => 'Es Teh Manis 1L',        'unit' => 'botol',  'buy' => 4000,   'sell' => 7000],
            ['name' => 'Nasi Uduk Komplit',      'unit' => 'porsi',  'buy' => 12000,  'sell' => 18000],
            ['name' => 'Ayam Goreng',            'unit' => 'potong', 'buy' => 12000,  'sell' => 17000],
            ['name' => 'Tempe Mendoan',          'unit' => 'porsi',  'buy' => 5000,   'sell' => 8000],
            ['name' => 'Sambal Goreng',          'unit' => 'bungkus','buy' => 8000,   'sell' => 12000],
            ['name' => 'Pisang Goreng',          'unit' => 'porsi',  'buy' => 8000,   'sell' => 12000],
        ];

        $product = fake()->randomElement($products);
        $name    = $product['name'] . ' - ' . fake()->randomLetter() . fake()->randomNumber(3);

        return [
            'category_id'      => Category::inRandomOrder()->first()?->id ?? Category::factory(),
            'partner_id'       => Partner::inRandomOrder()->first()?->id ?? Partner::factory(),
            'name'             => $product['name'],
            'slug'             => Str::slug($name),
            'description'      => fake()->optional(0.7)->paragraph(),
            'buy_price'        => $product['buy'],
            'sell_price'       => $product['sell'],
            // NULL = pakai default komisi dari partner
            'commission_type'  => null,
            'commission_value' => null,
            'unit'             => $product['unit'],
            'image'            => null,
            'is_active'        => true,
            'is_featured'      => false,
            'sort_order'       => 0,
        ];
    }

    public function featured(): static
    {
        return $this->state(fn () => ['is_featured' => true]);
    }

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

    public function forPartner(Partner $partner): static
    {
        return $this->state(fn () => ['partner_id' => $partner->id]);
    }

    public function forCategory(Category $category): static
    {
        return $this->state(fn () => ['category_id' => $category->id]);
    }
}
