<?php

declare(strict_types=1);

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Models\DashboardPreference;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class DashboardPreferenceController extends Controller
{
    public function updateShortcuts(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'pinned_spaces' => ['present', 'array', 'max:' . DashboardPreference::MAX_PINNED_SPACES],
            'pinned_spaces.*' => ['string'],
        ]);

        $pinnedSpaces = DashboardPreference::sanitizePinnedSpaces($validated['pinned_spaces'] ?? []);

        DashboardPreference::query()->updateOrCreate(
            ['user_id' => $request->user()->id],
            ['pinned_spaces' => $pinnedSpaces]
        );

        return response()->json([
            'data' => [
                'pinned_spaces' => $pinnedSpaces,
            ],
        ]);
    }
}