<?php

namespace Gaco\SnapBi\Services;

use Gaco\SnapBi\Contracts\ConfigInterface;
use Gaco\SnapBi\Services\Config\OcbcConfig;
use Gaco\SnapBi\Services\Config\BcaConfig;
use Gaco\SnapBi\Services\Config\MandiriConfig;
use Gaco\SnapBi\Services\Config\CimbConfig;
use Gaco\SnapBi\Exceptions\SnapBiException;

/**
 * Config Manager
 * 
 * Manages configuration for multiple banks
 * Similar to php-snap-bi pattern
 */
final class Config
{
    /**
     * Register configs for each bank
     *
     * @return array<string, string>
     */
    static function registeredConfigs(): array
    {
        return [
            'ocbc' => OcbcConfig::class,
            'bca' => BcaConfig::class,
            'mandiri' => MandiriConfig::class,
            'cimb' => CimbConfig::class,
            // Register new bank configs here
        ];
    }

    /**
     * Get config by bank name
     *
     * @param string $name Bank name (ocbc, bca, mandiri, etc.)
     * @return ConfigInterface
     * @throws SnapBiException
     */
    static function for(string $name): ConfigInterface
    {
        $configs = self::registeredConfigs();
        $name = strtolower($name);

        if (!isset($configs[$name])) {
            throw new SnapBiException("SNAP BI config for '{$name}' is not registered. Available: " . implode(', ', array_keys($configs)));
        }

        return $configs[$name]::call();
    }

    /**
     * Magic method to get config by bank name
     * Example: Config::ocbc() or Config::ocbc([...]) to register
     *
     * @param string $method Bank name
     * @param array|null $args Config array to register (optional)
     * @return ConfigInterface
     */
    static function __callStatic(string $method, array $args = null): ConfigInterface
    {
        $config = self::for($method);

        // If config array is provided, register it
        if ($args && isset($args[0]) && is_array($args[0])) {
            $config->register($args[0]);
        }

        return $config;
    }
}

