<?php

namespace App\Modules\HR\Employees\Requests;

use App\Modules\HR\Employees\Enums\EmploymentHistoryType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class StoreEmploymentHistoryRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('update', $this->route('employee')) ?? false;
    }

    /** @return array<string,mixed> */
    public function rules(): array
    {
        return [
            'type' => ['required', Rule::in(EmploymentHistoryType::values())],
            'effective_date' => ['required', 'date'],
            'previous_value' => ['nullable', 'string', 'max:255'],
            'new_value' => ['nullable', 'string', 'max:255'],
            'reason' => ['nullable', 'string', 'max:255'],
            'notes' => ['nullable', 'string'],
            'meta' => ['nullable', 'array'],
            'meta.sync_employee' => ['sometimes', 'boolean'],
            'meta.salary' => ['nullable', 'numeric', 'min:0'],
            'meta.department_id' => ['nullable', 'integer', 'exists:departments,id'],
            'meta.position_id' => ['nullable', 'integer', 'exists:positions,id'],
        ];
    }
}
