<?php

declare(strict_types=1);

namespace App\Rules\Wiki\Submission\Video;

use App\Constants\FeatureConstants;
use App\Rules\Wiki\Submission\SubmissionRule;
use Closure;
use Illuminate\Support\Arr;
use Illuminate\Translation\PotentiallyTranslatedString;
use Laravel\Pennant\Feature;

class VideoColorTransferStreamRule extends SubmissionRule
{
    /**
     * @param  Closure(string): PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (Feature::for(null)->active(FeatureConstants::IGNORE_ALL_FILE_VALIDATIONS)) {
            return;
        }

        $video = Arr::first(
            $this->streams(),
            fn (array $stream): bool => Arr::get($stream, 'codec_type') === 'video'
        );

        if (! in_array(Arr::get($video, 'color_transfer'), explode(',', (string) Feature::for(null)->value(FeatureConstants::VIDEO_COLOR_TRANSFER_STREAM)))) {
            $fail(__('validation.submission.video_color_transfer'));
        }
    }
}
