<?php

namespace App\Http\Requests;

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

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

    public function rules(): array
    {
        $baseRules = [];
        $currentRoute = Route::currentRouteName();

        $routeSpecificRules = [
            'users.index' => [
                'fk_site' => ['required', 'integer', 'min:1'],
                'fk_office' => ['required', 'integer', 'min:1']
            ],
            'users.store' => [
                'fk_site' => ['required', 'integer', 'min:1'],
                'fk_office' => ['required', 'integer', 'min:1'],
                'username' => ['required', 'string', 'min:3'],
                'password' => ['required', 'string', 'min:8'],
                'fk_collaborator' => ['nullable'],
                'fk_role' => ['required', 'integer', 'min:1']
            ],
            'users.update' => [
                'id' => ['required', 'integer', 'min:1'],
                'fk_office' => ['required', 'integer', 'min:1'],
                'username' => ['required', 'string', 'min:3'],
                'fk_collaborator' => ['nullable'],
                'fk_role' => ['required', 'integer', 'min:1']
            ],
            'users.destroy' => [
                'id' => ['required', 'integer', 'min:1'],
                'fk_office' => ['required', 'integer', 'min:1'],
            ],
            'users.reset' => [
                'id' => ['required', 'integer', 'min:1'],
                'password' => ['required', 'string', 'min:8'],
            ],
            'users.changeStatus' => [
                'id' => ['required', 'integer', 'min:1'],
                'fk_office' => ['required', 'integer', 'min:1'],
                'available' => ['required', 'boolean']
            ],
            'users.changeVisibility' => [
                'id' => ['required', 'integer', 'min:1'],
                'fk_office' => ['required', 'integer', 'min:1'],
                'visible' => ['required', 'boolean']
            ],
            'users.getUsersByRole' => [
                'fk_role' => ['required', 'integer', 'min:1']
            ],
            'users.show' => [
                'id' => ['required', 'integer', 'min:1'],
                'fk_office' => ['required', 'integer', 'min:1'],
            ],
            'users.list' => [
                'fk_site' => ['required', 'integer', 'min:1', 'exists:sy_sites,id'],
            ],
        ];

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