<?php

namespace Database\Factories;

use App\Enums\SubscriptionPlan;
use App\Enums\SubscriptionStatus;
use App\Models\Subscription;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends Factory<Subscription>
 */
class SubscriptionFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $plan = fake()->randomElement(SubscriptionPlan::cases());

        return [
            'full_name' => fake()->name(),
            'phone' => '9'.fake()->numerify('#########'),
            'email' => fake()->safeEmail(),
            'whatsapp' => fake()->optional()->boolean()
                ? '9'.fake()->numerify('#########')
                : null,
            'address' => fake()->address().' '.fake()->sentence(),
            'city' => fake()->city(),
            'pincode' => fake()->numerify('######'),
            'landmark' => fake()->optional()->streetName(),
            'plan' => $plan,
            'plan_price' => $plan->price(),
            'start_date' => fake()->dateTimeBetween('now', '+1 month'),
            'special_instructions' => fake()->optional()->sentence(),
            'terms_agreed' => true,
            'terms_agreed_at' => now(),
            'promo_emails' => fake()->boolean(),
            'ip_address' => fake()->ipv4(),
            'user_agent' => fake()->userAgent(),
            'order_id' => 'PVF-SUB-'.strtoupper(Str::random(8)),
            'order_status' => SubscriptionStatus::Pending,
        ];
    }

    /**
     * Indicate that the subscriber chose the basic plan.
     */
    public function basic(): static
    {
        return $this->state(fn (array $attributes) => [
            'plan' => SubscriptionPlan::Basic,
            'plan_price' => SubscriptionPlan::Basic->price(),
        ]);
    }

    /**
     * Indicate that the subscriber chose the premium plan.
     */
    public function premium(): static
    {
        return $this->state(fn (array $attributes) => [
            'plan' => SubscriptionPlan::Premium,
            'plan_price' => SubscriptionPlan::Premium->price(),
        ]);
    }

    /**
     * Indicate that the subscriber chose the luxury plan.
     */
    public function luxury(): static
    {
        return $this->state(fn (array $attributes) => [
            'plan' => SubscriptionPlan::Luxury,
            'plan_price' => SubscriptionPlan::Luxury->price(),
        ]);
    }

    /**
     * Indicate that the order is pending.
     */
    public function pending(): static
    {
        return $this->state(fn (array $attributes) => [
            'order_status' => SubscriptionStatus::Pending,
        ]);
    }

    /**
     * Indicate that the order is confirmed.
     */
    public function confirmed(): static
    {
        return $this->state(fn (array $attributes) => [
            'order_status' => SubscriptionStatus::Confirmed,
        ]);
    }

    /**
     * Indicate that the order is active.
     */
    public function active(): static
    {
        return $this->state(fn (array $attributes) => [
            'order_status' => SubscriptionStatus::Active,
        ]);
    }

    /**
     * Indicate that the order is paused.
     */
    public function paused(): static
    {
        return $this->state(fn (array $attributes) => [
            'order_status' => SubscriptionStatus::Paused,
        ]);
    }

    /**
     * Indicate that the order is cancelled.
     */
    public function cancelled(): static
    {
        return $this->state(fn (array $attributes) => [
            'order_status' => SubscriptionStatus::Cancelled,
        ]);
    }

    /**
     * Indicate that the order is completed.
     */
    public function completed(): static
    {
        return $this->state(fn (array $attributes) => [
            'order_status' => SubscriptionStatus::Completed,
        ]);
    }
}
