<?php

namespace App\Rules;

use App\Exceptions\InvalidPolicyTypeException;
use App\Services\BackupScheduling\PolicyTypeResolver;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;

class SupportedPolicyTypesRule implements ValidationRule
{
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $types = array_values((array) $value);

        try {
            app(PolicyTypeResolver::class)->ensureSupported($types);
        } catch (InvalidPolicyTypeException $exception) {
            $fail(sprintf('Unknown policy types: %s.', implode(', ', $exception->unknownPolicyTypes)));
        }
    }
}
