<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class UnsignedInteger implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $normalized = str_replace('.', '', (string) $value);

        if (! ctype_digit($normalized)) {
            $fail(':Attribute harus berupa bilangan bulat positif.');

            return;
        }

        $intValue = (int) $normalized;

        if ($intValue < 0 || $intValue > 4294967295) {
            $fail(':Attribute harus berada di antara 0 dan 4.294.967.295.');
        }
    }
}
