<?php

namespace Database\Factories;

use App\Enums\BookingStatus;
use App\Enums\PaymentMethod;
use App\Enums\PaymentType;
use App\Models\Booking;
use App\Models\Customer;
use App\Models\Floor;
use App\Models\UnitSubtype;
use App\Models\User;
use App\Support\BookingNumber;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends Factory<Booking>
 */
class BookingFactory extends Factory
{
    /**
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            'number' => app(BookingNumber::class)->unique(),
            'customer_id' => Customer::factory()->customer(),
            'agent_id' => null,
            'unit_subtype_id' => UnitSubtype::factory(),
            'unit_id' => fn (array $attributes): int => UnitSubtype::query()
                ->findOrFail($attributes['unit_subtype_id'])
                ->unit_id,
            'floor_id' => Floor::factory(),
            'unit_number' => strtoupper(fake()->bothify('?-##')),
            'payment_type' => PaymentType::DownPayment,
            'down_payment' => fake()->randomFloat(2, 100_000, 2_000_000),
            'received_amount' => fake()->randomFloat(2, 50_000, 1_000_000),
            'payment_method' => PaymentMethod::Cash,
            'payment_reference' => null,
            'account_name' => null,
            'status' => BookingStatus::Confirmed,
            'created_by' => User::factory(),
        ];
    }

    public function pending(): static
    {
        return $this->state(fn (array $attributes): array => [
            'status' => BookingStatus::Pending,
            'payment_type' => null,
            'down_payment' => null,
            'received_amount' => null,
            'payment_method' => null,
            'payment_reference' => null,
            'account_name' => null,
        ]);
    }

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