import { defineCommand } from 'citty';
import { resolveProfileApiUrl } from '../lib/api-base.js';
import { ApiClient } from '../lib/api-client.js';
import { setQuiet } from '../lib/output.js';
import { resolveProfile } from '../lib/resolvers.js';

interface CatalogItem {
  id: string;
  name: string;
  description: string;
  isInstalled: boolean;
  toolCount: number;
}

export async function runConnectorCatalog(
  client: Pick<ApiClient, 'get'>,
  args: { json: boolean },
  print: (line: string) => void,
): Promise<void> {
  const items = await client.get<CatalogItem[]>('/api/connectors/catalog');
  if (args.json) {
    print(JSON.stringify(items, null, 2));
    return;
  }
  for (const it of items) {
    const status = it.isInstalled ? 'installed' : 'available';
    print(`${it.id.padEnd(16)}  ${status.padEnd(10)}  ${it.name}`);
  }
}

export default defineCommand({
  meta: { name: 'catalog', description: 'list catalog entries' },
  args: {
    profile: { type: 'string', description: 'profile name', required: false },
    json: { type: 'boolean', description: 'emit raw JSON', default: false },
    quiet: { type: 'boolean', description: 'minimal output' },
  },
  async run({ args }) {
    if (args.quiet) setQuiet(true);
    const { name: profile } = await resolveProfile(args.profile as string | undefined);
    const baseUrl = await resolveProfileApiUrl(profile);
    const client = new ApiClient({ baseUrl });
    await runConnectorCatalog(client, { json: !!args.json }, (line) => console.log(line));
  },
});
