<?php

declare(strict_types=1);

use App\Models\DashboardPreference;
use App\Models\User;

it('persists sanitized pinned dashboard spaces for the authenticated user', function () {
    $user = User::factory()->create([
        'email_verified_at' => now(),
    ]);

    $response = $this->actingAs($user)->putJson('/api/dashboard/preferences/shortcuts', [
        'pinned_spaces' => [
            '/dashboard/notifications',
            '/dashboard/notifications',
            '/dashboard/comments/received',
            '/not-allowed',
            '/studio',
        ],
    ]);

    $response
        ->assertOk()
        ->assertJson([
            'data' => [
                'pinned_spaces' => [
                    '/dashboard/notifications',
                    '/dashboard/comments/received',
                    '/studio',
                ],
            ],
        ]);

    expect(DashboardPreference::query()->find($user->id)?->pinned_spaces)->toBe([
        '/dashboard/notifications',
        '/dashboard/comments/received',
        '/studio',
    ]);
});

it('allows clearing all pinned dashboard spaces for the authenticated user', function () {
    $user = User::factory()->create([
        'email_verified_at' => now(),
    ]);

    DashboardPreference::query()->create([
        'user_id' => $user->id,
        'pinned_spaces' => [
            '/dashboard/notifications',
        ],
    ]);

    $response = $this->actingAs($user)->putJson('/api/dashboard/preferences/shortcuts', [
        'pinned_spaces' => [],
    ]);

    $response
        ->assertOk()
        ->assertJson([
            'data' => [
                'pinned_spaces' => [],
            ],
        ]);

    expect(DashboardPreference::query()->find($user->id)?->pinned_spaces)->toBe([]);
});