import { Command, Flags } from '@oclif/core';
import chalk from 'chalk';
import { discoverContexts } from '../../ai/domain/reader.js';
import { createAiSetupPorts } from '../../ai/factories/provider.factory.js';
import { AiSetupService } from '../../ai/services/ai-setup.service.js';
export default class AiSetup extends Command {
    static description = 'Generate AI provider instruction files and skill symlinks from .ai/ source of truth.';
    static examples = [
        '<%= config.bin %> ai setup --all',
        '<%= config.bin %> ai setup --provider claude',
        '<%= config.bin %> ai setup --provider claude --provider opencode',
        '<%= config.bin %> ai setup --provider claude --target backend',
        '<%= config.bin %> ai setup --all --dry-run',
        '<%= config.bin %> ai setup --all --force',
    ];
    static flags = {
        all: Flags.boolean({
            default: false,
            description: 'Configure all available providers',
        }),
        'dry-run': Flags.boolean({
            default: false,
            description: 'Show what would be generated without writing files',
        }),
        force: Flags.boolean({
            char: 'f',
            default: false,
            description: 'Overwrite existing files without confirmation',
        }),
        provider: Flags.string({
            char: 'p',
            description: 'Provider(s) to configure (repeatable)',
            multiple: true,
            options: ['claude', 'codex', 'copilot', 'gemini', 'opencode'],
        }),
        target: Flags.string({
            char: 't',
            description: 'Generate only for a specific target directory',
        }),
    };
    async run() {
        const { flags } = await this.parse(AiSetup);
        const rootDir = process.cwd();
        const contexts = discoverContexts(rootDir, flags.target);
        if (contexts.length === 0) {
            this.error('No .ai/CONTEXT.md files found. Create .ai/CONTEXT.md in the project root or target directories.');
        }
        this.log(chalk.cyan(`Detected ${contexts.length} context(s): ${contexts.map((c) => c.name).join(', ')}`));
        const ports = createAiSetupPorts();
        const service = new AiSetupService(ports);
        const result = await service.execute({
            all: flags.all,
            contexts,
            dryRun: flags['dry-run'],
            force: flags.force,
            providers: flags.provider,
            rootDir,
        });
        for (const msg of result.messages) {
            this.log(msg);
        }
        if (result.filesWritten > 0 || result.symlinksCreated > 0) {
            this.log(chalk.green('\nDone.'));
        }
    }
}
