import { Command } from 'commander';
import { RegistryClient, readCredentials } from '@expxagents/registry-client';
export const searchCommand = new Command('search')
    .description('Search squads in the registry')
    .argument('<query>', 'Search query')
    .option('--category <cat>', 'Filter by category')
    .option('--limit <n>', 'Max results', '10')
    .action(async (query, options) => {
    const creds = readCredentials();
    const client = new RegistryClient({
        registryUrl: creds?.registryUrl || 'https://expxagents-marketplace-production.up.railway.app',
    });
    try {
        const result = await client.search({
            q: query,
            category: options.category,
            limit: parseInt(options.limit),
        });
        if (result.squads.length === 0) {
            console.log('No squads found.');
            return;
        }
        console.log(`Found ${result.total} squad(s):\n`);
        for (const squad of result.squads) {
            const stars = squad.rating > 0 ? ` \u2605${squad.rating.toFixed(1)}` : '';
            console.log(`  ${squad.fullName}${stars}`);
            console.log(`    ${squad.description || 'No description'}`);
            console.log(`    v${squad.latestVersion || '?'} | \u2193${squad.downloads}\n`);
        }
    }
    catch (err) {
        console.error(`\u2717 Search failed: ${err.message}`);
        process.exit(1);
    }
});
