<?php

namespace App\Rules;

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

class SkipOrUuidRule implements ValidationRule
{
    public function __construct(public bool $allowSkip = true) {}

    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (! is_string($value)) {
            $fail('Please enter SKIP or a valid UUID.');

            return;
        }

        $value = trim($value);

        if ($value === '') {
            $fail('Please enter SKIP or a valid UUID.');

            return;
        }

        if ($this->allowSkip && strtoupper($value) === 'SKIP') {
            return;
        }

        if (! Str::isUuid($value)) {
            $fail('Please enter SKIP or a valid UUID.');
        }
    }
}
