import { runtimeList } from '@agentuity/server';
import { z } from 'zod';
import { getCommand } from '../../../../command-prefix';
import { getGlobalCatalystAPIClient } from '../../../../config';
import * as tui from '../../../../tui';
import { createCommand } from '../../../../types';

const RuntimeInfoSchema = z.object({
	id: z.string().describe('Runtime ID'),
	name: z.string().describe('Runtime name'),
	description: z.string().optional().describe('Runtime description'),
});

const RuntimeListResponseSchema = z.object({
	runtimes: z.array(RuntimeInfoSchema).describe('List of runtimes'),
	total: z.number().describe('Total number of runtimes'),
});

export const listSubcommand = createCommand({
	name: 'list',
	aliases: ['ls'],
	description: 'List available sandbox runtimes',
	tags: ['read-only', 'slow', 'requires-auth'],
	requires: { auth: true, org: true },
	idempotent: true,
	examples: [
		{
			command: getCommand('cloud sandbox runtime list'),
			description: 'List all available runtimes',
		},
	],
	schema: {
		options: z.object({
			limit: z.number().optional().describe('Maximum number of results'),
			offset: z.number().optional().describe('Offset for pagination'),
			orgId: z.string().optional().describe('filter by organization id'),
			sort: z
				.enum(['name', 'created'])
				.optional()
				.describe('field to sort by (default: created)'),
			direction: z.enum(['asc', 'desc']).optional().describe('sort direction (default: desc)'),
		}),
		response: RuntimeListResponseSchema,
	},

	async handler(ctx) {
		const { opts, options, orgId: ctxOrgId, logger, auth, config } = ctx;
		const client = await getGlobalCatalystAPIClient(
			logger,
			auth,
			config?.name,
			undefined,
			config
		);
		const effectiveOrgId = opts?.orgId || ctxOrgId;

		const result = await runtimeList(client, {
			orgId: effectiveOrgId,
			limit: opts.limit,
			offset: opts.offset,
			sort: opts.sort,
			direction: opts.direction,
		});

		if (!options.json) {
			if (result.runtimes.length === 0) {
				tui.info('No runtimes available');
			} else {
				const tableData = result.runtimes.map((runtime) => {
					return {
						ID: runtime.id,
						Name: runtime.name,
						Description: runtime.description ?? '-',
					};
				});
				tui.table(tableData, [
					{ name: 'ID', alignment: 'left' },
					{ name: 'Name', alignment: 'left' },
					{ name: 'Description', alignment: 'left' },
				]);

				tui.info(`Total: ${result.total} ${tui.plural(result.total, 'runtime', 'runtimes')}`);
			}
		}

		return result;
	},
});

export default listSubcommand;
