<?php

namespace Modules\Feedback\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class FeedbackRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $rules = [
            'type' => 'required|in:bug,feature_request,general_feedback,ui_issue,performance_issue',
            'title' => 'required|string|max:255',
            'description' => 'required|string',
            'priority' => 'required|in:low,medium,high,critical',
            'page_url' => 'nullable|url',
            'steps_to_reproduce' => 'nullable|string',
            'expected_behavior' => 'nullable|string',
            'actual_behavior' => 'nullable|string',
            'additional_notes' => 'nullable|string',
            'images' => 'nullable|array',
            'images.*' => 'file|mimes:jpeg,png,jpg,gif,webp|max:10240', // 10MB max
            'videos' => 'nullable|array',
            'videos.*' => 'file|mimes:mp4,mov,webm|max:51200', // 50MB max
        ];

        // For update operations, add admin-specific fields
        if ($this->isMethod('PUT') || $this->isMethod('PATCH')) {
            $rules = [
                'type' => 'sometimes|in:bug,feature_request,general_feedback,ui_issue,performance_issue',
                'title' => 'sometimes|string|max:255',
                'description' => 'sometimes|string',
                'priority' => 'sometimes|in:low,medium,high,critical',
                'page_url' => 'nullable|url',
                'steps_to_reproduce' => 'nullable|string',
                'expected_behavior' => 'nullable|string',
                'actual_behavior' => 'nullable|string',
                'additional_notes' => 'nullable|string',
                'status' => 'sometimes|in:open,in_progress,resolved,closed,rejected',
                'assigned_to' => 'nullable|exists:users,id',
                'admin_notes' => 'nullable|string',
            ];
        }

        return $rules;
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'type.required' => 'Jenis maklum balas diperlukan.',
            'type.in' => 'Jenis maklum balas tidak sah.',
            'title.required' => 'Tajuk diperlukan.',
            'title.max' => 'Tajuk tidak boleh melebihi 255 aksara.',
            'description.required' => 'Penerangan diperlukan.',
            'priority.required' => 'Keutamaan diperlukan.',
            'priority.in' => 'Keutamaan tidak sah.',
            'page_url.url' => 'URL halaman tidak sah.',
            'images.*.mimes' => 'Format imej mestilah jpeg, png, jpg, gif atau webp.',
            'images.*.max' => 'Saiz imej tidak boleh melebihi 10MB.',
            'videos.*.mimes' => 'Format video mestilah mp4, mov atau webm.',
            'videos.*.max' => 'Saiz video tidak boleh melebihi 50MB.',
            'status.in' => 'Status tidak sah.',
            'assigned_to.exists' => 'Pengguna yang ditugaskan tidak wujud.',
        ];
    }
}
