<?php

namespace App\Http\Requests;

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

class RClientRequest extends FormRequest
{
    protected $rules = [];

    public function __construct()
    {
        $this->rules = [
            'fk_site' => ['required', 'integer', 'exists:sy_sites,id'],
            'fk_office' => ['required', 'integer', 'exists:sy_offices,id']
        ];
    }

    public function authorize(): bool
    {
        return true;
    }

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

        $routeRules = [
            'requestclient.index' => [],
            'requestclient.store' => [
                'firstname' => ['required', 'string', 'max:30'],
                'lastname' => ['required', 'string', 'max:30'],
                'secondlastname' => ['nullable', 'string', 'max:30'],
                'curp' => ['nullable', 'string', 'size:18'],
                'fk_city' => ['nullable', 'integer', 'exists:ct_cities,id'],
                'colony' => ['required', 'string', 'max:30'],
                'address' => ['required', 'string', 'max:50'],
                'phonenumber' => ['nullable', 'string', 'max:15'],
                'has_employee'=> ['nullable', 'boolean']
            ],
            'requestclient.update' => [
                'id' => ['required', 'integer', 'exists:sy_client_requests,id'],
                'firstname' => ['required', 'string', 'max:30'],
                'lastname' => ['required', 'string', 'max:30'],
                'secondlastname' => ['nullable', 'string', 'max:30'],
                'curp' => ['nullable', 'string', 'size:18'],
                'fk_city' => ['nullable', 'integer', 'exists:ct_cities,id'],
                'colony' => ['required', 'string', 'max:30'],
                'address' => ['required', 'string', 'max:50'],
                'phonenumber' => ['nullable', 'string', 'max:15'],
                'has_employee'=> ['nullable', 'boolean']
            ],
            'requestclient.destroy' => [
                'id' => ['required', 'integer', 'exists:sy_client_requests,id'],
            ],
            'requestclient.list' => [],
        ];

        return array_merge($this->rules, $routeRules[$currentRoute] ?? []);
    }
}
