<?php

namespace App\Modules\HR\Shifts\Requests;

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

class StoreEmployeeShiftAssignmentRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user()?->can('shifts.assign') ?? false;
    }

    /**
     * @return array<string, mixed>
     */
    public function rules(): array
    {
        $tenantId = $this->user()?->tenant_id;

        return [
            'employee_id' => [
                'required',
                'integer',
                Rule::exists('employees', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'shift_id' => [
                'required',
                'integer',
                Rule::exists('shifts', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'starts_at' => ['nullable', 'date'],
        ];
    }
}
