<?php

namespace Modules\LabCore\Entities\Services;

use Modules\LabCore\Entities\Contracts\LabDeviceDriver;

/**
 * In-memory registry of analyzer-vendor drivers.
 *
 * Each vendor module (NX600Lab, AbaxisLab, IdexxLab) registers its driver
 * from its service provider's boot() method:
 *
 *     DriverRegistry::register(new \Modules\NX600Lab\Entities\Drivers\Nx600Driver());
 *
 * LabCore then resolves the right driver for any lab_* row by its `vendor`
 * column. The registry degrades gracefully — if a vendor module is not
 * installed its driver is simply absent.
 */
class DriverRegistry
{
    /** @var array<string,LabDeviceDriver> */
    protected static array $drivers = [];

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

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

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

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

    /**
     * Registered vendor keys.
     *
     * @return array<int,string>
     */
    public static function vendors(): array
    {
        return array_keys(static::$drivers);
    }

    /**
     * Vendor key => human label, for dropdowns.
     *
     * @return array<string,string>
     */
    public static function options(): array
    {
        return array_map(fn(LabDeviceDriver $d) => $d->label(), static::$drivers);
    }

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