<?php

namespace Modules\Activity\Http\Requests;

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

class ActivityTypeRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $isCreate = $this->isMethod('POST');
        $isUpdate = $this->isMethod('PUT') || $this->isMethod('PATCH');

        if (! $isCreate && ! $isUpdate) {
            return [
                'name' => 'nullable|string|max:255',
                'code' => 'nullable|string|max:255',
            ];
        }

        $codeRule = Rule::unique('activity_types', 'code');

        if ($isUpdate) {
            $codeRule = $codeRule->ignore($this->route('activity_type'));
        }

        return [
            'name' => 'required|string|max:255',
            'code' => ['nullable', 'string', 'max:255', $codeRule],
        ];
    }

    public function messages(): array
    {
        return [
            'name.required' => 'Nama jenis aktiviti diperlukan.',
            'name.max' => 'Nama jenis aktiviti tidak boleh melebihi 255 aksara.',
            'code.max' => 'Kod jenis aktiviti tidak boleh melebihi 255 aksara.',
            'code.unique' => 'Kod jenis aktiviti sudah wujud.',
        ];
    }
}
