import type { GatewayAdapter } from './sdk/adapter-interface';
import { DtcSettlementAdapter } from './dtc-settlement/dtc-settlement-adapter';
import { DtccFiccAdapter } from './dtcc/dtcc-ficc-adapter';
import { DtccNsccAdapter } from './dtcc/dtcc-nscc-adapter';
import { KttEvidenceAdapter } from './ktt-evidence/ktt-evidence-adapter';
import { SwiftFinAdapter } from './swift-fin/swift-fin-adapter';
import { SwiftGpiAdapter } from './swift-gpi/swift-gpi-adapter';
import { SwiftIsoAdapter } from './swift-iso/swift-iso-adapter';
import { ThirdwebAdapter } from './thirdweb/thirdweb-adapter';
import { TtRouteAdapter } from './tt-route/tt-route-adapter';

/**
 * Canonical IDs for SolaceNet-maintained gateway rail adapters (see docs/solacenet/RAIL_AND_PROTOCOL_GOVERNANCE.md).
 * Implementations are scaffolds until replaced with production connectors.
 */
export const GATEWAY_RAIL_ADAPTER_IDS = [
	'dbis.adapter.swift-fin',
	'dbis.adapter.swift-iso',
	'dbis.adapter.swift-gpi',
	'dbis.adapter.dtc-settlement',
	'dbis.adapter.dtcc-nscc',
	'dbis.adapter.dtcc-ficc',
	'dbis.adapter.tt-route',
	'dbis.adapter.ktt-evidence',
	'dbis.adapter.thirdweb',
] as const;

export type GatewayRailAdapterId = (typeof GATEWAY_RAIL_ADAPTER_IDS)[number];

const factories: Record<GatewayRailAdapterId, () => GatewayAdapter> = {
	'dbis.adapter.swift-fin': () => new SwiftFinAdapter(),
	'dbis.adapter.swift-iso': () => new SwiftIsoAdapter(),
	'dbis.adapter.swift-gpi': () => new SwiftGpiAdapter(),
	'dbis.adapter.dtc-settlement': () => new DtcSettlementAdapter(),
	'dbis.adapter.dtcc-nscc': () => new DtccNsccAdapter(),
	'dbis.adapter.dtcc-ficc': () => new DtccFiccAdapter(),
	'dbis.adapter.tt-route': () => new TtRouteAdapter(),
	'dbis.adapter.ktt-evidence': () => new KttEvidenceAdapter(),
	'dbis.adapter.thirdweb': () => new ThirdwebAdapter(),
};

/** All registered rail adapter IDs (stable order). */
export function listGatewayRailAdapterIds(): GatewayRailAdapterId[] {
	return [...GATEWAY_RAIL_ADAPTER_IDS];
}

/** Instantiate a fresh adapter instance for the given ID (caller may `initialize()`). */
export function createGatewayRailAdapter(adapterId: string): GatewayAdapter | undefined {
	if (!isGatewayRailAdapterId(adapterId)) return undefined;
	return factories[adapterId]();
}

export function isGatewayRailAdapterId(id: string): id is GatewayRailAdapterId {
	return (GATEWAY_RAIL_ADAPTER_IDS as readonly string[]).includes(id);
}
