<?php

namespace App\Services\Inventory\DependencyTargets;

class FoundationTypeMap
{
    /**
     * @return array<string, array{inventory_policy_type:?string,label:string,linkable:bool,resolvable_via_db:bool}>
     */
    public function all(): array
    {
        return [
            'scope_tag' => [
                'inventory_policy_type' => 'roleScopeTag',
                'label' => 'Scope Tag',
                'linkable' => true,
                'resolvable_via_db' => true,
            ],
            'assignment_filter' => [
                'inventory_policy_type' => 'assignmentFilter',
                'label' => 'Assignment Filter',
                'linkable' => true,
                'resolvable_via_db' => true,
            ],
            'aad_group' => [
                'inventory_policy_type' => null,
                'label' => 'Group (external)',
                'linkable' => false,
                'resolvable_via_db' => false,
            ],
        ];
    }

    /**
     * @return array{inventory_policy_type:?string,label:string,linkable:bool,resolvable_via_db:bool}|null
     */
    public function get(?string $foundationType): ?array
    {
        if (! is_string($foundationType) || $foundationType === '') {
            return null;
        }

        return $this->all()[$foundationType] ?? null;
    }

    /**
     * @return list<string>
     */
    public function resolvableFoundationTypes(): array
    {
        return collect($this->all())
            ->filter(fn (array $row) => ($row['resolvable_via_db'] ?? false) === true)
            ->keys()
            ->values()
            ->all();
    }

    /**
     * @return array<string, string>
     */
    public function policyTypeToFoundationType(): array
    {
        $map = [];

        foreach ($this->all() as $foundationType => $row) {
            $policyType = $row['inventory_policy_type'] ?? null;
            if (is_string($policyType) && $policyType !== '') {
                $map[$policyType] = $foundationType;
            }
        }

        return $map;
    }
}
