<?php

namespace Tests\Feature\Livewire;

use App\Livewire\NotificationBell;
use App\Models\Notification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class NotificationBellTest extends TestCase
{
    use RefreshDatabase;

    public function test_it_renders_successfully(): void
    {
        Livewire::test(NotificationBell::class)
            ->assertStatus(200)
            ->assertSee('Notifications');
    }

    public function test_it_shows_unread_count_badge(): void
    {
        Notification::factory()->count(3)->unread()->create();

        Livewire::test(NotificationBell::class)
            ->assertSee('3');
    }

    public function test_it_hides_badge_when_no_unread(): void
    {
        Notification::factory()->count(2)->read()->create();

        Livewire::test(NotificationBell::class)
            ->assertDontSee('Mark all as read');
    }

    public function test_it_shows_empty_state_when_no_notifications(): void
    {
        Livewire::test(NotificationBell::class)
            ->assertSee('No notifications');
    }

    public function test_mark_as_read(): void
    {
        $notification = Notification::factory()->unread()->create();

        Livewire::test(NotificationBell::class)
            ->call('markAsRead', $notification->id);

        $this->assertTrue($notification->fresh()->isRead());
    }

    public function test_mark_all_as_read(): void
    {
        Notification::factory()->count(3)->unread()->create();

        $this->assertEquals(3, Notification::unread()->count());

        Livewire::test(NotificationBell::class)
            ->call('markAllAsRead');

        $this->assertEquals(0, Notification::unread()->count());
    }

    public function test_it_displays_notification_title_and_message(): void
    {
        Notification::factory()->create([
            'title' => 'Test Notification Title',
            'message' => 'Test notification message body',
        ]);

        Livewire::test(NotificationBell::class)
            ->assertSee('Test Notification Title')
            ->assertSee('Test notification message body');
    }

    public function test_it_caps_badge_at_99_plus(): void
    {
        Notification::factory()->count(100)->unread()->create();

        Livewire::test(NotificationBell::class)
            ->assertSee('99+');
    }
}
