<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Property;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Conversation>
 */
final class ConversationFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $property = Property::inRandomOrder()->first() ?? Property::factory()->create();
        $guest = User::role('guest')->inRandomOrder()->first() ?? User::factory()->create();

        return [
            'property_id' => $property->id,
            'guest_id' => $guest->id,
            'owner_id' => $property->owner_id,
            'last_message_at' => $this->faker->dateTimeBetween('-7 days', 'now'),
            'last_message_preview' => $this->faker->sentence(),
            'guest_unread_count' => $this->faker->numberBetween(0, 5),
            'owner_unread_count' => $this->faker->numberBetween(0, 5),
        ];
    }

    /**
     * Indicate that the conversation has no unread messages
     */
    public function allRead(): static
    {
        return $this->state(fn (array $attributes) => [
            'guest_unread_count' => 0,
            'owner_unread_count' => 0,
        ]);
    }

    /**
     * Indicate that the guest has unread messages
     */
    public function guestUnread(int $count = 1): static
    {
        return $this->state(fn (array $attributes) => [
            'guest_unread_count' => $count,
            'owner_unread_count' => 0,
        ]);
    }

    /**
     * Indicate that the owner has unread messages
     */
    public function ownerUnread(int $count = 1): static
    {
        return $this->state(fn (array $attributes) => [
            'guest_unread_count' => 0,
            'owner_unread_count' => $count,
        ]);
    }
}
