<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\User;
use App\Notifications\AccountApprovedNotification;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class NotificationsMarkReadTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_mark_own_notification_as_read(): void
    {
        $user = User::factory()->create();

        Sanctum::actingAs($user);

        $user->notify(new AccountApprovedNotification);

        $id = $user->notifications()->firstOrFail()->id;

        $this->postJson("/api/notifications/{$id}/mark-as-read")->assertOk();

        $notification = $user->notifications()->whereKey($id)->first();

        $this->assertNotNull($notification->read_at);
    }

    public function test_mark_read_returns_404_for_foreign_notification(): void
    {
        $alice = User::factory()->create();
        $bob = User::factory()->create();

        $alice->notify(new AccountApprovedNotification);

        $id = $alice->notifications()->firstOrFail()->id;

        Sanctum::actingAs($bob);

        $this->postJson("/api/notifications/{$id}/mark-as-read")->assertNotFound();
    }
}
