<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Affiliate;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/** @extends Factory<Affiliate> */
class AffiliateFactory extends Factory
{
    protected $model = Affiliate::class;

    /** @return array<string, mixed> */
    public function definition(): array
    {
        return [
            'user_id' => User::factory(),
            'referral_code' => Str::lower(Str::random(8)),
            'status' => 'active',
            'commission_type' => 'percentage',
            'commission_rate' => 10.00,
            'recurring_commissions' => false,
            'minimum_payout' => 50.00,
            'total_earned' => 0,
            'total_paid' => 0,
            'pending_balance' => 0,
            'approved_at' => now(),
        ];
    }

    public function pending(): static
    {
        return $this->state(fn (): array => [
            'status' => 'pending',
            'approved_at' => null,
        ]);
    }

    public function active(): static
    {
        return $this->state(fn (): array => [
            'status' => 'active',
            'approved_at' => now(),
        ]);
    }

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

    public function flat(): static
    {
        return $this->state(fn (): array => [
            'commission_type' => 'flat',
            'commission_rate' => 5.00,
        ]);
    }

    public function withEarnings(): static
    {
        return $this->state(fn (): array => [
            'total_earned' => fake()->randomFloat(2, 50, 500),
            'total_paid' => fake()->randomFloat(2, 0, 200),
            'pending_balance' => fake()->randomFloat(2, 10, 100),
        ]);
    }
}
