<?php

namespace App\Modules\HR\Compensation\Requests;

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

class CompensationRequest extends FormRequest
{
    public function authorize(): bool
    {
        $ability = $this->route('employee') ? 'compensation.manage'
            : ($this->isMethod('PUT') ? 'compensation.update' : 'compensation.create');

        return $this->user()?->can($ability) ?? false;
    }

    public function rules(): array
    {
        if ($this->route('employee')) {
            return [
                'type_id' => ['required', 'integer'], 'amount' => ['nullable', 'numeric', 'min:0'],
                'percentage' => ['nullable', 'numeric', 'min:0', 'max:100'],
                'effective_from' => ['required', 'date'], 'effective_to' => ['nullable', 'date', 'after_or_equal:effective_from'],
                'is_active' => ['sometimes', 'boolean'],
            ];
        }

        return [
            'code' => ['required', 'string', 'max:50'], 'name' => ['required', 'string', 'max:255'],
            'calculation_type' => ['required', Rule::in(['fixed', 'percentage'])],
            'default_amount' => ['nullable', 'numeric', 'min:0'], 'is_recurring' => ['sometimes', 'boolean'],
            'is_taxable' => ['sometimes', 'boolean'], 'is_active' => ['sometimes', 'boolean'],
        ];
    }
}
