<?php

namespace Modules\SmartStorage\Entities\Services;

use Modules\SmartStorage\Entities\Contracts\StorageBackendDriver;

/**
 * In-memory registry of storage backend drivers.
 *
 * Built-in drivers register from SmartStorageServiceProvider::boot(); a future
 * module could add its own backend the same way. The registry degrades
 * gracefully — a driver whose SDK is missing reports isAvailable() = false.
 */
class StorageDriverRegistry
{
    /** @var array<string,StorageBackendDriver> */
    protected static array $drivers = [];

    public static function register(StorageBackendDriver $driver): void
    {
        static::$drivers[$driver->key()] = $driver;
    }

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

    public static function has(string $key): bool
    {
        return isset(static::$drivers[$key]);
    }

    /** @return array<string,StorageBackendDriver> */
    public static function all(): array
    {
        return static::$drivers;
    }

    /**
     * key => label for dropdowns (all registered, available or not).
     *
     * @return array<string,string>
     */
    public static function options(): array
    {
        return array_map(fn (StorageBackendDriver $d) => $d->label(), static::$drivers);
    }

    public static function flush(): void
    {
        static::$drivers = [];
    }
}
