import { Command, Args } from '@oclif/core';
import chalk from 'chalk';
import { getWorkspaceInfo } from '../../lib/agents/commands.js';
import { isValidAgentName, normalizeAgentName } from '../../lib/themes.js';
import { getTheme, addThemeNames, getThemeNames } from '../../lib/database/index.js';
import { machineOutputFlags } from '../../lib/pmo/index.js';
import { shouldOutputJson } from '../../lib/prompt-json.js';
export default class ThemeAddNames extends Command {
    static description = 'Add names to a theme';
    static examples = [
        '<%= config.bin %> <%= command.id %> greek-gods zeus athena poseidon',
        '<%= config.bin %> <%= command.id %> my-theme agent-a agent-b',
    ];
    static flags = {
        ...machineOutputFlags,
    };
    static args = {
        theme: Args.string({
            description: 'Theme ID',
            required: true,
        }),
        names: Args.string({
            description: 'Names to add to the theme (space-separated)',
            required: false,
        }),
    };
    static strict = false; // Allow multiple name arguments
    async run() {
        const { args, argv, flags } = await this.parse(ThemeAddNames);
        const jsonMode = shouldOutputJson(flags);
        try {
            const workspaceInfo = getWorkspaceInfo();
            // Validate theme exists
            const theme = getTheme(workspaceInfo.path, args.theme);
            if (!theme) {
                this.error(`Theme "${args.theme}" not found. Run "prlt theme list" to see available themes.`);
            }
            // Get names from remaining arguments (skip the theme arg)
            const rawNames = argv.slice(1);
            if (rawNames.length === 0) {
                this.error('Please provide at least one name to add.');
            }
            // Normalize and validate names
            const validNames = [];
            const normalized = [];
            for (const name of rawNames) {
                const normalizedName = normalizeAgentName(name);
                if (normalizedName && isValidAgentName(normalizedName)) {
                    validNames.push(normalizedName);
                    if (normalizedName !== name) {
                        normalized.push({ original: name, normalized: normalizedName });
                    }
                }
            }
            if (validNames.length === 0) {
                this.error('No valid names to add after normalization.');
            }
            // Show normalizations
            if (normalized.length > 0) {
                this.log(chalk.blue('Normalized names:'));
                for (const { original, normalized: norm } of normalized) {
                    this.log(chalk.dim(`   ${original} → ${norm}`));
                }
            }
            // Add names to theme
            addThemeNames(workspaceInfo.path, theme.id, validNames);
            // Get updated count
            const allNames = getThemeNames(workspaceInfo.path, theme.id);
            if (jsonMode) {
                this.log(JSON.stringify({ type: 'success', result: { theme: args.theme, added: validNames, totalNames: allNames.length } }, null, 2));
                return;
            }
            this.log(chalk.green(`\n Added ${validNames.length} name(s) to ${theme.display_name}:`));
            this.log(chalk.gray(`   ${validNames.join(', ')}`));
            this.log(chalk.gray(`\n   Theme now has ${allNames.length} names total.`));
        }
        catch (error) {
            this.error(error instanceof Error ? error.message : String(error));
        }
    }
}
//# sourceMappingURL=add-names.js.map