<?php

namespace Database\Factories;

use App\Enums\ConsultationStatus;
use App\Enums\ConsultationType;
use App\Enums\PaymentStatus;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Consultation>
 */
class ConsultationFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $consultationType = fake()->randomElement(ConsultationType::cases());
        $paymentStatus = $consultationType === ConsultationType::Paid
            ? fake()->randomElement([PaymentStatus::Pending, PaymentStatus::Received])
            : PaymentStatus::NotApplicable;

        return [
            'user_id' => User::factory(),
            'booking_date' => fake()->dateTimeBetween('now', '+30 days'),
            'booking_time' => fake()->time('H:i:s'),
            'problem_summary' => fake()->paragraph(),
            'consultation_type' => $consultationType,
            'payment_amount' => $consultationType === ConsultationType::Paid ? fake()->randomFloat(2, 50, 500) : null,
            'payment_status' => $paymentStatus,
            'status' => fake()->randomElement(ConsultationStatus::cases()),
            'admin_notes' => null,
        ];
    }

    /**
     * Create a free consultation.
     */
    public function free(): static
    {
        return $this->state(fn (array $attributes) => [
            'consultation_type' => ConsultationType::Free,
            'payment_amount' => null,
            'payment_status' => PaymentStatus::NotApplicable,
        ]);
    }

    /**
     * Create a paid consultation.
     */
    public function paid(): static
    {
        return $this->state(fn (array $attributes) => [
            'consultation_type' => ConsultationType::Paid,
            'payment_amount' => fake()->randomFloat(2, 50, 500),
            'payment_status' => fake()->randomElement([PaymentStatus::Pending, PaymentStatus::Received]),
        ]);
    }

    /**
     * Create a pending consultation.
     */
    public function pending(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => ConsultationStatus::Pending,
        ]);
    }

    /**
     * Create an approved consultation.
     */
    public function approved(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => ConsultationStatus::Approved,
        ]);
    }

    /**
     * Create a completed consultation.
     */
    public function completed(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => ConsultationStatus::Completed,
        ]);
    }

    /**
     * Create a cancelled consultation.
     */
    public function cancelled(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => ConsultationStatus::Cancelled,
        ]);
    }

    /**
     * Create a no-show consultation.
     */
    public function noShow(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => ConsultationStatus::NoShow,
        ]);
    }

    /**
     * Create a rejected consultation.
     */
    public function rejected(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => ConsultationStatus::Rejected,
        ]);
    }

    /**
     * Create a guest consultation (no user account).
     */
    public function guest(): static
    {
        return $this->state(fn (array $attributes) => [
            'user_id' => null,
            'guest_name' => fake()->name(),
            'guest_email' => fake()->unique()->safeEmail(),
            'guest_phone' => fake()->phoneNumber(),
        ]);
    }
}
