<?php

declare(strict_types=1);

namespace Database\Seeders;

use App\Models\Booking;
use App\Models\Review;
use App\Models\User;
use App\Notifications\Bookings\BookingCancelledNotification;
use App\Notifications\Bookings\BookingConfirmedNotification;
use App\Notifications\Bookings\BookingDeclinedNotification;
use App\Notifications\Bookings\BookingRequestedNotification;
use App\Notifications\Reviews\ReviewPostedNotification;
use Illuminate\Database\Seeder;

final class NotificationSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->command->info('Seeding notifications...');

        // Get users
        $guests = User::role('guest')->limit(5)->get();
        $owners = User::role('owner')->limit(5)->get();

        if ($guests->isEmpty() || $owners->isEmpty()) {
            $this->command->warn('No guests or owners found. Please seed users first.');

            return;
        }

        // Get some bookings
        $bookings = Booking::with(['guest', 'property.owner'])
            ->inRandomOrder()
            ->limit(10)
            ->get();

        if ($bookings->isEmpty()) {
            $this->command->warn('No bookings found. Please seed bookings first.');

            return;
        }

        $notificationCount = 0;

        // Create booking requested notifications
        foreach ($bookings->take(3) as $booking) {
            // Notify guest
            $booking->guest->notify(new BookingRequestedNotification($booking, 'guest'));
            $notificationCount++;

            // Notify owner
            $booking->property->owner->notify(new BookingRequestedNotification($booking, 'owner'));
            $notificationCount++;
        }

        // Create booking confirmed notifications
        foreach ($bookings->skip(3)->take(2) as $booking) {
            $booking->guest->notify(new BookingConfirmedNotification($booking));
            $notificationCount++;
        }

        // Create booking declined notifications
        foreach ($bookings->skip(5)->take(2) as $booking) {
            $booking->guest->notify(new BookingDeclinedNotification($booking));
            $notificationCount++;
        }

        // Create booking cancelled notifications
        foreach ($bookings->skip(7)->take(2) as $booking) {
            // Notify guest
            $booking->guest->notify(new BookingCancelledNotification($booking, 'guest'));
            $notificationCount++;

            // Notify owner
            $booking->property->owner->notify(new BookingCancelledNotification($booking, 'owner'));
            $notificationCount++;
        }

        // Get some reviews
        $reviews = Review::with(['guest', 'property.owner'])
            ->inRandomOrder()
            ->limit(3)
            ->get();

        // Create review posted notifications
        foreach ($reviews as $review) {
            $review->property->owner->notify(new ReviewPostedNotification($review));
            $notificationCount++;
        }

        $this->command->info("Created {$notificationCount} notifications successfully!");
    }
}
