<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Route;

class CollaboratorRequest extends FormRequest
{
    public function authorize(): bool
    {
        return true;
    }

    public function rules(): array
    {
        $baseRules = [
            'fk_site' => ['required', 'integer', 'exists:sy_sites,id'],
            'fk_office' => ['required', 'integer', 'exists:sy_offices,id']
        ];
        $currentRoute = Route::currentRouteName();

        $routeSpecificRules = [
            'collaborator.index' => [],
            'collaborator.store' => [
                'firstname' => ['required', 'string', 'min:3'],
                'lastname' => ['required', 'string', 'min:3'],
                'secondlastname' => ['string', 'min:3'],
                'commissionpercent' => ['required', 'numeric', 'min:0'],
                'fk_position' => ['sometimes', 'nullable', 'integer', 'exists:ct_positions,id']
            ],
            'collaborator.update' => [
                'id' => ['required', 'integer', 'min:1'],
                'firstname' => ['required', 'string', 'min:3'],
                'lastname' => ['required', 'string', 'min:3'],
                'secondlastname' => ['string', 'min:3'],
                'commissionpercent' => ['required', 'numeric', 'min:0'],
                'fk_position' => ['sometimes', 'nullable', 'integer', 'exists:ct_positions,id']
            ],
            'collaborator.changeStatus' => [
                'id' => ['required', 'integer', 'min:1'],
                'available' => ['required', 'boolean']
            ],
            'collaborator.changeVisibleStatus' => [
                'id' => ['required', 'integer', 'min:1'],
                'visible' => ['required', 'boolean']
            ],
            'collaborator.destroy' => [
                'id' => ['required', 'integer', 'min:1'],
            ],
            'collaborator.show' => [
                'id' => ['required', 'integer', 'min:1'],
            ],
            'collaborator.list' => [
                'positionnames' => ['sometimes', 'array', 'min:1']
            ]
        ];

        return array_merge($baseRules, $routeSpecificRules[$currentRoute] ?? []);
    }
}
