<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreDocumentTemplateRequest extends FormRequest
{
    public function authorize(): bool
    {
        $user = $this->user();

        return $user && ($user->hasPermission('manage-document-templates') || $user->hasPermission('manage-settings') || $user->hasRole('admin'));
    }

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            // Slug uniqueness enforced only for first version; controller will increment version if slug exists
            'slug' => ['required', 'string', 'max:255'],
            'custom_name' => ['nullable', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'file' => ['required', 'file', 'mimetypes:application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'max:4096'],
            'meta' => ['sometimes', 'array'],
            'meta.*' => ['nullable'],
            'action_id' => ['nullable', 'integer', 'exists:actions,id'], // New optional field
            'decision_id' => ['nullable', 'integer', 'exists:decisions,id'], // New optional field
            'activity_note_template' => ['nullable', 'string'], // New optional field
        ];
    }

    public function messages(): array
    {
        return [
            'file.mimetypes' => 'Datoteka mora biti DOCX.',
            'file.max' => 'Datoteka je prevelika (max 4MB).',
        ];
    }
}
