<?php

namespace App\Http\Requests;

use App\FieldVisibility\FieldCatalogueRegistry;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class UpdateFieldVisibilityRequest extends FormRequest
{
    public function authorize(): bool
    {
        return Auth::check()
            && (Auth::user()->hasRole('Administrator') || Auth::user()->hasRole('mtc'));
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $entity = (string) $this->route('entity');
        $registry = app(FieldCatalogueRegistry::class);
        $hideable = $registry->has($entity) ? $registry->for($entity)->hideableKeys() : [];

        return [
            'hidden' => ['present', 'array'],
            'hidden.*' => ['string', Rule::in($hideable)],
        ];
    }
}
