<?php

namespace App\Http\Requests;

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

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

    public function rules(): array
    {
        $route = Route::getCurrentRoute()->getName();
        $rules = [
            'fk_site' => ['required', 'integer', 'exists:sy_sites,id'],
            'fk_office' => ['required', 'integer', 'exists:sy_offices,id']
        ];

        $routeRules = [
            'group.index' => [],
            'group.store' => [
                'fk_zone' => ['sometimes', 'integer', 'exists:sy_zones,id'],
                'groupname' => ['required', 'string', 'min:2', 'max:35'],
                'fk_promoter' => ['sometimes', 'integer', 'exists:ct_collaborators,id']
            ],
            'group.update' => [
                'id' => ['required', 'integer', 'exists:sy_groups,id'],
                'fk_zone' => ['sometimes', 'integer', 'exists:sy_zones,id'],
                'groupname' => ['required', 'string', 'min:2', 'max:35'],
                'fk_promoter' => ['sometimes', 'integer', 'exists:ct_collaborators,id']
            ],
            'group.destroy' => [
                'id' => ['required', 'integer', 'exists:sy_groups,id'],
            ],
            'group.changeStatus' => [
                'id' => ['required', 'integer', 'exists:sy_groups,id'],
                'available' => ['required', 'boolean']
            ],
            'group.changeVisibility' => [
                'id' => ['required', 'integer', 'exists:sy_groups,id'],
                'visible' => ['required', 'boolean']
            ],
            'group.list' => [
                'fk_zones' => ['sometimes', 'nullable', 'array', 'min:1']
            ],
            'group.show' => [
                'id' => ['required', 'integer', 'exists:sy_groups,id'],
            ],
            'group.showPromoter' => [
                'id' => ['required', 'integer', 'exists:sy_groups,id'],
            ],
        ];

        return array_merge($rules, $routeRules[$route]);
    }
}
