<?php

namespace App\Modules\Settings\Localization\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateTranslationRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('translations.update', $this->route('translation')) ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $id = $this->route('translation')?->id;

        return [
            'key' => [
                'required', 'string', 'max:255',
                Rule::unique('translations', 'key')
                    ->where(fn ($q) => $q->where('locale', $this->input('locale')))
                    ->ignore($id),
            ],
            'group' => ['nullable', 'string', 'max:100'],
            'locale' => ['required', 'string', 'max:10', 'exists:languages,code'],
            'value' => ['required', 'string'],
        ];
    }
}
