<?php

namespace App\Modules\HR\Assets\Requests;

use App\Modules\HR\Assets\Models\Asset;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class AssetActionRequest extends FormRequest
{
    public function authorize(): bool
    {
        $asset = $this->route('asset');

        return $asset instanceof Asset && ($this->user()?->can('manage', $asset) ?? false);
    }

    public function rules(): array
    {
        $employeeRequired = in_array($this->route()->getName(), ['hr.assets.assign', 'hr.assets.transfer'], true);

        return [
            'employee_id' => [
                $employeeRequired ? 'required' : 'nullable',
                'integer',
                Rule::exists('employees', 'id')->where(fn ($q) => $q->where('tenant_id', $this->user()?->tenant_id)),
            ],
            'notes' => ['nullable', 'string', 'max:5000'],
        ];
    }
}
