<?php

namespace Bakgul\Evaluator\Evaluators;

use Bakgul\Kernel\Helpers\Arr;
use Bakgul\Kernel\Helpers\Settings;
use Bakgul\Kernel\Helpers\Isolation;
use Bakgul\Evaluator\Evaluator;
use Bakgul\Evaluator\Tasks\SetDefaultMessage;

class HasUnknownRole extends Evaluator
{
    const KEY = 'role';
    const TYPE = 'unknown';

    private static $issue = [];

    public static function handle(array $request): ?array
    {
        return parent::evaluatePart(get_called_class(), $request);
    }

    public static function args($request): array
    {
        return [self::KEY, self::TYPE, $request, self::$issue, false, SetDefaultMessage::class];
    }

    public static function hasIssue(array $request): bool
    {
        self::findIssues($request['type']);

        return !empty(self::$issue);
    }

    private static function findIssues($type)
    {
        self::$issue = Arr::unique([...self::$issue, ...self::issuedRoles($type)]);
    }

    private static function issuedRoles($type): array
    {
        $type = [Isolation::type($type), Isolation::extra($type)];

        return self::isValid($type) ? [] : [$type[1]];
    }

    private static function isValid(array $type): bool
    {
        return in_array($type[1], Settings::files("{$type[0]}.roles"));
    }
}
