<?php

namespace App\Http\Requests;

use App\Concerns\EmployeeDocumentValidationRules;
use App\Concerns\ValidatesStaffDesignation;
use App\Models\PermanentEmployee;
use App\Support\EmployeeShift;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdatePermanentEmployeeRequest extends FormRequest
{
    use EmployeeDocumentValidationRules, ValidatesStaffDesignation;

    public function authorize(): bool
    {
        return (bool) $this->user()?->can('permanent-employees.update');
    }

    protected function prepareForValidation(): void
    {
        /** @var PermanentEmployee $employee */
        $employee = $this->route('permanentEmployee');

        $this->merge([
            'shift' => $this->input('shift', $employee->shift ?? EmployeeShift::MORNING),
            'is_active' => $this->boolean('is_active'),
        ]);
    }

    /**
     * @return array<string, array<int, mixed>>
     */
    public function rules(): array
    {
        /** @var PermanentEmployee $employee */
        $employee = $this->route('permanentEmployee');

        return [
            'designation_id' => $this->designationIdRules($employee->designation_id),
            'full_name' => ['required', 'string', 'max:255'],
            'father_name' => ['required', 'string', 'max:255'],
            'national_card_number' => [
                'required',
                'string',
                'max:50',
                Rule::unique(PermanentEmployee::class, 'national_card_number')->ignore($employee->id),
            ],
            'phone_number' => ['required', 'string', 'max:30'],
            'relative_phone_number' => ['nullable', 'string', 'max:30'],
            'address' => ['required', 'string', 'max:1000'],
            'monthly_salary' => ['required', 'numeric', 'min:0'],
            'joining_date' => ['required', 'date'],
            'shift' => ['required', 'string', Rule::in(EmployeeShift::values())],
            'is_active' => ['required', 'boolean'],
            'image' => $this->employeeImageRules(required: false),
            'national_card' => $this->employeeDocumentRules(required: false),
            'health_card' => $this->employeeDocumentRules(required: false),
            'labor_card' => $this->employeeDocumentRules(required: false),
            'contract' => $this->employeeDocumentRules(required: false),
        ];
    }
}
