<?php

namespace App\Http\Controllers;

use App\Models\Followee;
use App\Models\Notification;
use App\Models\User;
use App\Settings\SettingsRegistry;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Gate;
use Inertia\Inertia;

class UserController extends Controller
{

    public function settings(?string $category = null){
        $allowedTabs = ['general', 'followers'];
        $user    = auth()->user();
        $current = array_merge(SettingsRegistry::defaults(), $user->settings ?? []);
        $fields  = array_map(fn($field) => array_merge($field, [
            'value' => $current[$field['key']] ?? $field['default'],
        ]), SettingsRegistry::schema());

        return Inertia::render('UserSettings', [
            'fields' => $fields,
            'categories' => SettingsRegistry::categories(),
            'defaultTab' => in_array($category, $allowedTabs, true) ? $category : 'general',
        ]);
    }
}
