<?php

namespace App\Rules;

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

class HasCorrectAnswer implements ValidationRule
{
    /**
     * Run the validation rule.
     *
     * @param  \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        $correctAnswers = false;
        foreach ($value as $singleArray) {
            if ($singleArray['is_true'] == '1') {
                $correctAnswers = true;
                break;
            }
        }
        if (!$correctAnswers) {
            $fail('The :attribute must have at least 1 correct answer.');
        }
    }
}
