<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class VoucherFactory extends Factory
{
    public function definition(): array
    {
        $type = fake()->randomElement(['percent', 'fixed']);

        return [
            'code'         => strtoupper(Str::random(6)),
            'type'         => $type,
            'value'        => $type === 'percent'
                ? fake()->randomElement([5, 10, 15, 20])
                : fake()->randomElement([5000, 10000, 15000, 20000]),
            'min_order'    => fake()->randomElement([0, 20000, 30000, 50000]),
            'max_discount' => $type === 'percent' ? fake()->randomElement([10000, 15000, 20000, null]) : null,
            'quota'        => fake()->randomElement([10, 20, 50, null]),
            'used_count'   => 0,
            'valid_from'   => now()->toDateString(),
            'valid_until'  => now()->addDays(30)->toDateString(),
            'is_active'    => true,
        ];
    }

    public function expired(): static
    {
        return $this->state(fn () => [
            'valid_until' => now()->subDay()->toDateString(),
        ]);
    }

    public function unlimited(): static
    {
        return $this->state(fn () => ['quota' => null]);
    }

    public function percentDiscount(float $percent = 10, float $maxDiscount = null): static
    {
        return $this->state(fn () => [
            'type'         => 'percent',
            'value'        => $percent,
            'max_discount' => $maxDiscount,
        ]);
    }

    public function fixedDiscount(float $amount = 10000): static
    {
        return $this->state(fn () => [
            'type'  => 'fixed',
            'value' => $amount,
        ]);
    }
}
