<?php

namespace App\Modules\HR\Shifts\Requests;

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

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

    public function rules(): array
    {
        $tenantId = $this->user()?->tenant_id;

        return [
            'name' => ['required', 'string', 'max:255'],
            'days' => ['required', 'array', 'min:1', 'max:90'],
            'days.*.day_number' => ['required', 'integer', 'min:1', 'distinct'],
            'days.*.shift_id' => [
                'nullable', 'integer',
                Rule::exists('shifts', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'days.*.is_day_off' => ['sometimes', 'boolean'],
            'employee_ids' => ['sometimes', 'array'],
            'employee_ids.*' => [
                'integer', 'distinct',
                Rule::exists('employees', 'id')->where(fn ($q) => $q->where('tenant_id', $tenantId)),
            ],
            'starts_at' => ['required_with:employee_ids', 'date'],
        ];
    }
}
