<?php

namespace App\Http\Requests;

use Mtc\MercuryDataModels\User;

class UpdateSpecDocument extends SpecDocumentRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        return [
            'title' => ['sometimes', 'string', 'max:255'],
            'version' => ['sometimes', 'nullable', 'string', 'max:50'],
            // The author must be an mtc user (only they can own a spec document).
            'author_id' => [
                'sometimes',
                'nullable',
                function (string $attribute, mixed $value, \Closure $fail): void {
                    if ($value !== null && ! User::query()->role('mtc')->whereKey($value)->exists()) {
                        $fail('The selected author must be an mtc user.');
                    }
                },
            ],
        ];
    }
}
