<?php

declare(strict_types=1);

namespace Database\Factories;

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

/**
 * @extends Factory<Plan>
 */
class PlanFactory extends Factory
{
    protected $model = Plan::class;

    public function definition(): array
    {
        $name = $this->faker->randomElement(['Basic', 'Professional', 'Enterprise', 'Starter']);

        return [
            'name' => $name,
            'slug' => Str::slug($name).'-'.$this->faker->unique()->numberBetween(1, 1000),
            'description' => $this->faker->sentence(),
            'price' => $this->faker->randomElement([50000, 100000, 200000, 500000]),
            'annual_price' => null,
            'billing_cycle' => 'monthly',
            'currency_code' => 'COP',
            'max_users' => $this->faker->randomElement([5, 10, 25, 0]),
            'max_collectors' => $this->faker->randomElement([2, 5, 10, 0]),
            'max_active_credits' => $this->faker->randomElement([100, 500, 1000, 0]),
            'max_monthly_volume' => $this->faker->randomElement([10000000, 50000000, 100000000, 0]),
            'max_clients' => $this->faker->randomElement([50, 200, 500, 0]),
            'features' => [],
            'has_pwa_access' => true,
            'is_active' => true,
            'is_public' => true,
            'is_trial_available' => true,
            'trial_days' => 14,
            'sort_order' => $this->faker->numberBetween(1, 10),
            'badge_text' => null,
            'badge_color' => $this->faker->randomElement(['gray', 'primary', 'success', 'warning']),
        ];
    }

    /**
     * Basic plan configuration.
     */
    public function basic(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => 'Basic',
            'slug' => 'basic',
            'price' => 50000,
            'max_users' => 3,
            'max_collectors' => 2,
            'max_active_credits' => 50,
            'max_clients' => 30,
            'badge_color' => 'gray',
        ]);
    }

    /**
     * Professional plan configuration.
     */
    public function professional(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => 'Professional',
            'slug' => 'professional',
            'price' => 150000,
            'max_users' => 10,
            'max_collectors' => 5,
            'max_active_credits' => 200,
            'max_clients' => 100,
            'badge_color' => 'primary',
        ]);
    }

    /**
     * Enterprise plan configuration.
     */
    public function enterprise(): static
    {
        return $this->state(fn (array $attributes) => [
            'name' => 'Enterprise',
            'slug' => 'enterprise',
            'price' => 500000,
            'max_users' => 0, // Unlimited
            'max_collectors' => 0,
            'max_active_credits' => 0,
            'max_clients' => 0,
            'badge_color' => 'success',
        ]);
    }

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

    /**
     * Annual billing cycle.
     */
    public function annual(): static
    {
        return $this->state(fn (array $attributes) => [
            'billing_cycle' => 'annual',
            'annual_price' => $attributes['price'] * 10, // 2 months discount
        ]);
    }
}
