<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\TaxRate;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<TaxRate>
 */
class TaxRateFactory extends Factory
{
    protected $model = TaxRate::class;

    private static int $factoryIndex = 0;

    /** @return array<string, mixed> */
    public function definition(): array
    {
        $countries = ['US', 'GB', 'DE', 'FR', 'CA', 'AU', 'NL', 'SE', 'JP', 'BR'];
        $country = $countries[self::$factoryIndex % count($countries)];
        self::$factoryIndex++;

        return [
            'name' => fake()->words(3, true).' Tax',
            'country_code' => $country,
            'region_code' => null,
            'rate' => fake()->randomFloat(2, 1, 30),
            'type' => fake()->randomElement(['inclusive', 'exclusive']),
            'priority' => 0,
            'is_active' => true,
        ];
    }

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

    public function inclusive(): static
    {
        return $this->state(fn (array $attributes): array => [
            'type' => 'inclusive',
        ]);
    }

    public function exclusive(): static
    {
        return $this->state(fn (array $attributes): array => [
            'type' => 'exclusive',
        ]);
    }

    public function forCountry(string $countryCode, ?string $regionCode = null): static
    {
        return $this->state(fn (array $attributes): array => [
            'country_code' => strtoupper($countryCode),
            'region_code' => $regionCode,
        ]);
    }
}
