import select from '@inquirer/select';
import { Args, Command, Flags } from '@oclif/core';
import ora from 'ora';
import { BACK_PACKAGES, FRONT_PACKAGES, } from '../../generator/domain/config.js';
import { addBackPackage } from '../../generator/handlers/back.handler.js';
import { addFrontPackage } from '../../generator/handlers/front.handler.js';
export default class Add extends Command {
    static args = {
        scope: Args.string({
            description: 'Scope where the command will act.',
            options: ['back', 'front'],
            required: true,
        }),
    };
    static description = 'Add an Aurora package to an existing project.';
    static examples = [
        '<%= config.bin %> add back',
        '<%= config.bin %> add front',
        '<%= config.bin %> add back --force',
    ];
    static flags = {
        force: Flags.boolean({
            char: 'f',
            default: false,
            description: 'Overwrite files that already exist (default: skip existing files).',
        }),
        verbose: Flags.boolean({
            char: 'v',
            default: false,
            description: 'Report all steps on screen.',
        }),
    };
    async run() {
        const { args, flags } = await this.parse(Add);
        switch (args.scope) {
            case 'back': {
                const definition = await this.selectPackageDefinition(BACK_PACKAGES);
                const spinner = ora(`Installing ${definition.name} package...`).start();
                try {
                    const postInstallMessage = await addBackPackage(definition.packageFolder, flags);
                    spinner.succeed(`Backend package "${definition.name}" added.`);
                    if (postInstallMessage)
                        this.log(postInstallMessage);
                }
                catch (error) {
                    spinner.fail(`Failed to install backend package "${definition.name}": ${error.message}`);
                    throw error;
                }
                break;
            }
            case 'front': {
                const definition = await this.selectPackageDefinition(FRONT_PACKAGES);
                const spinner = ora(`Installing ${definition.name} package...`).start();
                try {
                    const postInstallMessage = await addFrontPackage(definition.packageFolder, flags);
                    spinner.succeed(`Frontend package "${definition.name}" added.`);
                    if (postInstallMessage)
                        this.log(postInstallMessage);
                }
                catch (error) {
                    spinner.fail(`Failed to install frontend package "${definition.name}": ${error.message}`);
                    throw error;
                }
                break;
            }
        }
    }
    async selectPackageDefinition(packages) {
        const packageFolder = await select({
            choices: packages.map((pkg) => ({
                name: pkg.name,
                value: pkg.packageFolder,
            })),
            message: 'Select a package to add:',
        });
        return packages.find((p) => p.packageFolder === packageFolder);
    }
}
