<?php

namespace Modules\User\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class EmploymentRequest 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
    {
        // Check if this is a create or update operation
        $isCreate = $this->isMethod('POST');
        $isUpdate = $this->isMethod('PUT') || $this->isMethod('PATCH');

        // If this is not a POST, PUT, or PATCH request, return minimal rules
        if (! $isCreate && ! $isUpdate) {
            return [
                'company_name' => 'nullable|string|max:255',
                'job_title' => 'nullable|string|max:255',
                'employment_type' => 'nullable|string|max:255',
                'salary' => 'nullable|numeric|min:0',
                'start_date' => 'nullable|date',
                'end_date' => 'nullable|date',
                'is_current' => 'nullable|boolean',
            ];
        }

        // If this is an update operation, make email and password optional
        if ($isUpdate) {
            return [
                'company_name' => 'required|string|max:255',
                'job_title' => 'required|string|max:255',
                'employment_type' => 'required|string|max:255',
                'salary' => 'required|numeric|min:0',
                'start_date' => 'required|date',
                'end_date' => 'nullable|date',
                'is_current' => 'required|boolean',
            ];
        }

        // Default rules for create operations
        return [
            'company_name' => 'required|string|max:255',
            'job_title' => 'required|string|max:255',
            'employment_type' => 'required|string|max:255',
            'salary' => 'required|numeric|min:0',
            'start_date' => 'required|date',
            'end_date' => 'nullable|date',
            'is_current' => 'required|boolean',
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'company_name.required' => 'Nama syarikat diperlukan.',
            'company_name.max' => 'Nama syarikat tidak boleh melebihi 255 aksara.',
            'job_title.required' => 'Jawatan diperlukan.',
            'job_title.max' => 'Jawatan tidak boleh melebihi 255 aksara.',
            'employment_type.required' => 'Jenis kerja diperlukan.',
            'employment_type.max' => 'Jenis kerja tidak boleh melebihi 255 aksara.',
            'salary.required' => 'Gaji diperlukan.',
            'salary.numeric' => 'Gaji tidak sah.',
            'salary.min' => 'Gaji tidak boleh kurang dari 0.',
            'start_date.required' => 'Tarikh mulai kerja diperlukan.',
            'start_date.date' => 'Tarikh mulai kerja tidak sah.',
            'end_date.date' => 'Tarikh akhir kerja tidak sah.',
            'is_current.required' => 'Status kerja saat ini diperlukan.',
            'is_current.boolean' => 'Status kerja saat ini tidak sah.',
        ];
    }
}
