<?php

namespace Modules\SaasSwitcher\Support;

/**
 * Reads the SaaS project catalogue from config('saasswitcher.projects').
 * Connections for each project are registered by SaasSwitcherServiceProvider.
 */
class ProjectRegistry
{
    public static function all(): array
    {
        return config('saasswitcher.projects', []);
    }

    public static function exists(string $key): bool
    {
        return array_key_exists($key, static::all());
    }

    public static function get(string $key): ?array
    {
        return static::all()[$key] ?? null;
    }

    public static function connection(string $key): ?string
    {
        return static::all()[$key]['connection'] ?? null;
    }

    /** The host project (its DB is the default `mysql` connection / super-admin home). */
    public static function host(): ?array
    {
        foreach (static::all() as $p) {
            if (! empty($p['is_host'])) {
                return $p;
            }
        }
        return null;
    }
}
