<?php

declare(strict_types=1);

namespace Database\Factories;

use App\Models\Review;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Review>
 */
final class ReviewFactory extends Factory
{
    protected $model = Review::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        // Generate a random overall rating
        $rating = $this->faker->randomFloat(1, 3.5, 5.0);

        // Generate category ratings (slightly varied from overall)
        $cleanlinessRating = $this->faker->randomFloat(1, max(1.0, $rating - 0.5), min(5.0, $rating + 0.5));
        $accuracyRating = $this->faker->randomFloat(1, max(1.0, $rating - 0.5), min(5.0, $rating + 0.5));
        $communicationRating = $this->faker->randomFloat(1, max(1.0, $rating - 0.5), min(5.0, $rating + 0.5));
        $locationRating = $this->faker->randomFloat(1, max(1.0, $rating - 0.5), min(5.0, $rating + 0.5));
        $valueRating = $this->faker->randomFloat(1, max(1.0, $rating - 0.5), min(5.0, $rating + 0.5));

        // Positive review titles and comments
        $positiveTitles = [
            'Amazing stay!',
            'Highly recommend',
            'Perfect getaway',
            'Excellent property',
            'Would definitely return',
            'Beautiful place',
            'Great experience',
            'Fantastic location',
            'Wonderful host',
            'Exceeded expectations',
        ];

        $positiveComments = [
            'We had an absolutely wonderful time at this property. Everything was exactly as described and the host was very responsive. The location was perfect and we enjoyed every moment of our stay.',
            'This property exceeded our expectations in every way. Clean, comfortable, and beautifully decorated. The host provided excellent recommendations for local restaurants and activities.',
            'Perfect place for a relaxing getaway. The property was spotless, well-equipped, and in a great location. We would definitely book again!',
            'Outstanding property with amazing amenities. The host was incredibly helpful and made check-in seamless. Highly recommend to anyone visiting the area.',
            'We loved our stay here! The property was even better in person than in the photos. Great communication from the host and everything we needed was provided.',
            'Fantastic location with easy access to everything. The property was clean, comfortable, and had all the amenities we needed. Would love to return!',
        ];

        return [
            'rating' => $rating,
            'cleanliness_rating' => $this->faker->boolean(70) ? $cleanlinessRating : null,
            'accuracy_rating' => $this->faker->boolean(70) ? $accuracyRating : null,
            'communication_rating' => $this->faker->boolean(70) ? $communicationRating : null,
            'location_rating' => $this->faker->boolean(70) ? $locationRating : null,
            'value_rating' => $this->faker->boolean(70) ? $valueRating : null,
            'title' => $this->faker->boolean(80) ? $this->faker->randomElement($positiveTitles) : null,
            'comment' => $this->faker->randomElement($positiveComments),
            'status' => 'approved',
            'is_featured' => $this->faker->boolean(10),
            'owner_response' => null,
            'owner_response_date' => null,
        ];
    }

    /**
     * Indicate that the review has an owner response.
     */
    public function withResponse(): static
    {
        $responses = [
            'Thank you so much for your kind words! We\'re thrilled you enjoyed your stay and hope to host you again soon.',
            'We appreciate your feedback and are glad you had a great experience. Come back anytime!',
            'Thank you for staying with us! We\'re happy to hear you enjoyed the property and look forward to welcoming you back.',
            'We\'re so pleased you had a wonderful time! Your feedback means a lot to us. Hope to see you again!',
        ];

        return $this->state(fn (array $attributes) => [
            'owner_response' => $this->faker->randomElement($responses),
            'owner_response_date' => now()->subDays($this->faker->numberBetween(1, 30)),
        ]);
    }

    /**
     * Indicate that the review is featured.
     */
    public function featured(): static
    {
        return $this->state(fn (array $attributes) => [
            'is_featured' => true,
            'rating' => $this->faker->randomFloat(1, 4.5, 5.0),
        ]);
    }
}
