/**
 * Upsert solacenet_provider_connector for the gateway rail plane (PG-SN-002).
 * Run: npx ts-node -r tsconfig-paths/register scripts/seed-solacenet-gateway-provider.ts
 * Requires DATABASE_URL and prisma client.
 */

import { v4 as uuidv4 } from 'uuid';
import type { Prisma } from '@prisma/client';
import prisma from '@/shared/database/prisma';
import { logger } from '@/infrastructure/monitoring/logger';

const CONNECTOR_ID = 'dbis-gateway-rail-plane';

async function main() {
	const metadata: Prisma.InputJsonValue = {
		capabilityId: 'gateway-microservices',
		governanceDoc: 'docs/solacenet/RAIL_AND_PROTOCOL_GOVERNANCE.md',
		note: 'Logical provider for SolaceNet-maintained gateway rail adapters; bindings use solacenet_capability_binding.',
	};

	const existing = await prisma.solacenet_provider_connector.findUnique({
		where: { connectorId: CONNECTOR_ID },
	});

	if (existing) {
		await prisma.solacenet_provider_connector.update({
			where: { connectorId: CONNECTOR_ID },
			data: {
				name: 'DBIS Gateway Rail Plane (SolaceNet)',
				providerType: 'gateway-rail',
				status: 'active',
				metadata,
			},
		});
		logger.info('Updated solacenet_provider_connector', { connectorId: CONNECTOR_ID });
		return;
	}

	await prisma.solacenet_provider_connector.create({
		data: {
			id: uuidv4(),
			connectorId: CONNECTOR_ID,
			name: 'DBIS Gateway Rail Plane (SolaceNet)',
			providerType: 'gateway-rail',
			status: 'active',
			metadata,
		},
	});
	logger.info('Created solacenet_provider_connector', { connectorId: CONNECTOR_ID });
}

if (require.main === module) {
	main()
		.catch((e) => {
			logger.error('seed-solacenet-gateway-provider failed', {
				error: e instanceof Error ? e.message : e,
			});
			process.exitCode = 1;
		})
		.finally(async () => {
			await prisma.$disconnect();
		});
}
