<?php

namespace KeyLair\TwoFactor;

use KeyLair\TwoFactor\Providers\TotpProvider;

/**
 * Central registry of available 2FA methods. To add a new method later
 * (e.g. when selling this software and a customer wants email-based 2FA):
 *
 *   1. Write a class implementing TwoFactorProviderInterface.
 *   2. Add it to $providers below, keyed by its methodName().
 *
 * No other code — login flow, admin settings UI, database schema — needs
 * to change, since everything is written against the interface.
 */
class TwoFactorManager
{
    /** @var array<string, class-string<TwoFactorProviderInterface>> */
    private static array $providers = [
        'totp' => TotpProvider::class,
        // 'email' => EmailProvider::class, // future
        // 'sms'   => SmsProvider::class,   // future
    ];

    public static function available(): array
    {
        $result = [];
        foreach (self::$providers as $key => $class) {
            $result[$key] = (new $class())->displayName();
        }
        return $result;
    }

    public static function get(string $method): ?TwoFactorProviderInterface
    {
        if (!isset(self::$providers[$method])) {
            return null;
        }

        return new self::$providers[$method]();
    }
}
