<?php

namespace Gaco\SnapBi\Services;

use Gaco\SnapBi\Services\Banks\Ocbc\OcbcSnapApi;
use Gaco\SnapBi\Services\Banks\Bca\BcaSnapApi;
use Gaco\SnapBi\Services\Banks\Mandiri\MandiriSnapApi;
use Gaco\SnapBi\Services\Banks\Cimb\CimbSnapApi;
use Gaco\SnapBi\Contracts\ServiceInterface;
use Gaco\SnapBi\Exceptions\SnapBiException;

/**
 * Bank Service Manager
 * 
 * Manages bank-specific service instances
 * Similar to php-snap-bi pattern
 */
final class BankManager
{
    /**
     * Register bank services
     *
     * @return array<string, string>
     */
    static function registeredBanks(): array
    {
        return [
            'ocbc' => OcbcSnapApi::class,
            'bca' => BcaSnapApi::class,
            'mandiri' => MandiriSnapApi::class,
            'cimb' => CimbSnapApi::class,
            // Register new bank services here
        ];
    }

    /**
     * Get bank service by name
     *
     * @param string $name Bank name (ocbc, bca, mandiri, etc.)
     * @param \Gaco\SnapBi\Contracts\ConfigInterface|null $config Bank config (optional)
     * @return ServiceInterface
     * @throws SnapBiException
     */
    static function for(string $name, ?\Gaco\SnapBi\Contracts\ConfigInterface $config = null): ServiceInterface
    {
        $banks = self::registeredBanks();
        $name = strtolower($name);

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

        $bankClass = $banks[$name];
        return new $bankClass($config);
    }
}

