<?php

namespace App\Http\Requests\Admin\Services\LetterRequest;

use App\Enums\LetterStatus;
use Illuminate\Foundation\Http\FormRequest;

class LetterRequestRequest extends FormRequest
{
    public function authorize(): bool
    {
        if ($this->isMethod('post')) {
            return $this->user()->can('create-letter-requests');
        }

        $letterRequest = $this->route('letter_request');

        return $this->user()->can('update-letter-requests')
            && $letterRequest->user_id === $this->user()->id
            && $letterRequest->status === LetterStatus::Submitted->value;
    }

    public function rules(): array
    {
        return [
            'letter_type' => ['required', 'string', 'max:100'],
            'purpose' => ['nullable', 'string'],
        ];
    }
}
