<?php

namespace Modules\Activity\Http\Requests;

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

class ActivityRequest 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 [
                'activity_type_id' => 'nullable|uuid|exists:activity_types,id',
                'title' => 'nullable|string|max:255',
                'reference_number' => 'nullable|string|max:255',
                'description' => 'nullable|string',
                'start_datetime' => 'nullable|date',
                'end_datetime' => 'nullable|date|after_or_equal:start_datetime',
                'organizer' => 'nullable|string|max:255',
                'location' => 'nullable|string|max:255',
                'is_active' => 'nullable|boolean',
            ];
        }

        $referenceRule = Rule::unique('activities', 'reference_number');

        if ($isUpdate) {
            $referenceRule = $referenceRule->ignore($this->route('activity'));
        }

        return [
            'activity_type_id' => 'required|uuid|exists:activity_types,id',
            'title' => 'required|string|max:255',
            'reference_number' => ['nullable', 'string', 'max:255', $referenceRule],
            'description' => 'nullable|string',
            'start_datetime' => 'nullable|date',
            'end_datetime' => 'nullable|date|after_or_equal:start_datetime',
            'organizer' => 'nullable|string|max:255',
            'location' => 'nullable|string|max:255',
            'is_active' => 'required|boolean',
            'documents' => 'nullable|array',
            'documents.*' => 'file|mimes:pdf,jpg,jpeg,png,doc,docx|max:10240',
            'gallery' => 'nullable|array',
            'gallery.*' => 'file|mimes:jpg,jpeg,png,webp,gif|max:10240',
        ];
    }

    public function messages(): array
    {
        return [
            'activity_type_id.required' => 'Jenis aktiviti diperlukan.',
            'activity_type_id.exists' => 'Jenis aktiviti tidak sah.',
            'title.required' => 'Tajuk aktiviti diperlukan.',
            'title.max' => 'Tajuk aktiviti tidak boleh melebihi 255 aksara.',
            'reference_number.unique' => 'Nombor rujukan sudah wujud.',
            'end_datetime.after_or_equal' => 'Tarikh tamat mestilah selepas atau sama dengan tarikh mula.',
            'is_active.required' => 'Status aktif diperlukan.',
            'is_active.boolean' => 'Status aktif tidak sah.',
            'documents.*.file' => 'Dokumen mestilah fail yang sah.',
            'documents.*.mimes' => 'Format dokumen tidak disokong.',
            'documents.*.max' => 'Saiz dokumen melebihi had maksimum 10MB.',
            'gallery.*.file' => 'Imej galeri mestilah fail yang sah.',
            'gallery.*.mimes' => 'Format imej galeri tidak disokong. Gunakan jpg, jpeg, png, webp atau gif.',
            'gallery.*.max' => 'Saiz imej galeri melebihi had maksimum 10MB.',
        ];
    }
}
