<?php

namespace Modules\Role\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Modules\Role\Entities\Role;

class RoleRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     */
    public function rules(): array
    {
        return [
            'name' => [
                'required',
                'string',
                'max:255'
            ],
            'guard_name' => [
                'required',
                'string',
                'in:api,web'
            ],
            'permissions' => [
                'sometimes',
                'array'
            ],
            'permissions.*' => [
                'required',
                'uuid',
                Rule::exists('permissions', 'id'),
            ],
            'fullname' => [
                'required',
                'string',
                'max:255',
            ],
            'context' => [
                'required',
                'string',
                Rule::in(['member', 'admin']),
            ],
        ];
    }

    /**
     * Configure the validator instance.
     */
    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            $roleId = $this->route('role');
            $name = $this->input('name');
            $guardName = $this->input('guard_name', 'api');
            
            // Check for unique name within the same guard
            $query = Role::where('name', $name)
                ->where('guard_name', $guardName);
            
            if ($roleId) {
                $query->where('id', '!=', $roleId);
            }
            
            if ($query->exists()) {
                $validator->errors()->add('name', 'This role name already exists for the selected guard.');
            }
        });
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        return [
            'name.required' => 'Role name is required.',
            'name.unique' => 'This role name already exists for the selected guard.',
            'guard_name.required' => 'Guard name is required.',
            'guard_name.in' => 'Guard name must be either api or web.',
            'permissions.array' => 'Permissions must be an array.',
            'permissions.*.uuid' => 'Each permission id must be a valid UUID.',
            'permissions.*.exists' => 'One or more selected permissions do not exist.',
            'fullname.required' => 'Fullname is required.',
            'fullname.string' => 'Fullname must be a string.',
            'fullname.max' => 'Fullname must be less than 255 characters.',
            'context.required' => 'Konteks peranan diperlukan.',
            'context.in' => 'Konteks peranan mestilah ahli atau pentadbir.',
        ];
    }
}
