import { capabilityRegistryService } from '@/core/solacenet/registry/capability-registry.service';
import { logger } from '@/infrastructure/monitoring/logger';

async function main() {
	try {
		logger.info('Registering DBIS Gateway Microservices capability...');
		await capabilityRegistryService.createCapability({
			capabilityId: 'gateway-microservices',
			name: 'DBIS Gateway Microservices',
			version: '1.0.0',
			description:
				'SolaceNet-maintained gateway rail adapters and HTTP surface; many connectors are scaffolds until production APIs (see marketplace copy + PROTOCOL_GAPS_CHECKLIST). Governance: docs/solacenet/RAIL_AND_PROTOCOL_GOVERNANCE.md',
			defaultState: 'enabled' as any,
			dependencies: ['ledger', 'iso20022', 'reconciliation'].filter(Boolean),
		});

		const subCapabilities = [
			{ capabilityId: 'gateway-edge', name: 'Gateway Edge Plane' },
			{ capabilityId: 'gateway-control', name: 'Gateway Control Plane' },
			{ capabilityId: 'gateway-operations', name: 'Gateway Operations Plane' },
			{ capabilityId: 'gateway-adapters', name: 'Gateway Adapter Plane' },
		];

		for (const sub of subCapabilities) {
			try {
				await capabilityRegistryService.createCapability({
					capabilityId: sub.capabilityId,
					name: sub.name,
					version: '1.0.0',
					description: sub.name,
					defaultState: 'enabled' as any,
					dependencies: ['gateway-microservices'],
				});
			} catch (err: any) {
				if (err?.message?.includes('already exists')) {
					logger.info(`Capability ${sub.capabilityId} already exists, skipping`);
				} else {
					throw err;
				}
			}
		}

		logger.info('Gateway capabilities registered.');
	} catch (error: any) {
		if (error?.message?.includes('already exists')) {
			logger.info('Capability gateway-microservices already exists, skipping');
		} else {
			logger.error('Failed to register gateway capability', { error: error?.message || error });
			process.exitCode = 1;
		}
	}
}

if (require.main === module) {
	// eslint-disable-next-line @typescript-eslint/no-floating-promises
	main();
}

