<?php

namespace App\Services\BackupScheduling;

use App\Exceptions\InvalidPolicyTypeException;
use Illuminate\Support\Arr;

class PolicyTypeResolver
{
    public function supportedPolicyTypes(): array
    {
        return Arr::pluck(config('tenantpilot.supported_policy_types', []), 'type');
    }

    public function ensureSupported(array $types): void
    {
        $unknown = $this->findUnknown($types);

        if (! empty($unknown)) {
            throw new InvalidPolicyTypeException($unknown);
        }
    }

    public function filterRuntime(array $types): array
    {
        $valid = $this->filter($types);

        return array_values($valid);
    }

    public function resolveRuntime(array $types): array
    {
        $valid = $this->filter($types);
        $unknown = $this->findUnknown($types);

        return [
            'valid' => array_values($valid),
            'unknown' => array_values($unknown),
        ];
    }

    protected function filter(array $types): array
    {
        $supported = $this->supportedPolicyTypes();

        return array_values(array_intersect($types, $supported));
    }

    protected function findUnknown(array $types): array
    {
        $supported = $this->supportedPolicyTypes();

        return array_values(array_diff($types, $supported));
    }
}
