import { Flags } from '@oclif/core';
import inquirer from 'inquirer';
import { PMOCommand, pmoBaseFlags } from '../../lib/pmo/index.js';
import { styles } from '../../lib/styles.js';
import { getWorkspaceInfo } from '../../lib/agents/commands.js';
import { openWorkspaceDatabase } from '../../lib/database/index.js';
import { loadExecutionConfig, saveTerminalApp, saveTerminalOpenInBackground, saveTmuxControlMode, saveShell, saveExecutionSetting, } from '../../lib/execution/config.js';
import { shouldOutputJson, outputSuccessAsJson, outputErrorAsJson, createMetadata, } from '../../lib/prompt-json.js';
export default class ExecutionConfig extends PMOCommand {
    static description = 'View and update execution preferences';
    static examples = [
        '<%= config.bin %> execution config                    # Interactive menu',
        '<%= config.bin %> execution config --json             # Output current config as JSON',
        '<%= config.bin %> execution config --list             # Show all settings',
        '<%= config.bin %> execution config --set defaultEnvironment host',
        '<%= config.bin %> execution config --set outputMode interactive',
        '<%= config.bin %> execution config --set permissionMode safe',
        '<%= config.bin %> execution config --setting outputMode --json  # Show output mode choices',
    ];
    static flags = {
        ...pmoBaseFlags,
        set: Flags.string({
            char: 's',
            description: 'Set a config value (format: key value)',
            multiple: true,
        }),
        list: Flags.boolean({
            char: 'l',
            description: 'List all configuration values',
            default: false,
        }),
        setting: Flags.string({
            description: 'Navigate to a specific setting prompt (for agent navigation)',
        }),
    };
    getPMOOptions() {
        return { promptIfMultiple: false };
    }
    async execute() {
        const { flags } = await this.parse(ExecutionConfig);
        const jsonMode = shouldOutputJson(flags);
        const jsonModeConfig = jsonMode ? { flags, commandName: 'execution config' } : null;
        // Get workspace info
        let workspaceInfo;
        try {
            workspaceInfo = getWorkspaceInfo();
        }
        catch {
            if (jsonMode) {
                outputErrorAsJson('NOT_IN_WORKSPACE', 'Not in a workspace. Run "prlt init" first.', createMetadata('execution config', flags));
                this.exit(1);
            }
            this.error('Not in a workspace. Run "prlt init" first.');
        }
        // Open database
        const db = openWorkspaceDatabase(workspaceInfo.path);
        try {
            // Load current config
            const config = loadExecutionConfig(db);
            // Handle --set flag
            if (flags.set && flags.set.length > 0) {
                for (const setValue of flags.set) {
                    const [key, ...valueParts] = setValue.split(' ');
                    const value = valueParts.join(' ');
                    if (!key || !value) {
                        if (jsonMode) {
                            outputErrorAsJson('INVALID_SET_FORMAT', `Invalid format: "${setValue}". Use: --set "key value"`, createMetadata('execution config', flags));
                        }
                        else {
                            this.error(`Invalid format: "${setValue}". Use: --set "key value"`);
                        }
                        continue;
                    }
                    this.setConfigValue(db, key, value, jsonMode);
                }
                db.close();
                return;
            }
            // Handle --list or --json flag without --setting (just show config)
            if ((flags.list || shouldOutputJson(flags)) && !flags.setting) {
                if (jsonMode) {
                    outputSuccessAsJson({
                        terminal: {
                            app: config.terminal.app,
                            openInBackground: config.terminal.openInBackground,
                        },
                        shell: config.shell,
                        tmux: {
                            controlMode: config.tmux.controlMode,
                        },
                        defaultEnvironment: config.defaultEnvironment,
                        defaultExecutor: config.defaultExecutor,
                        outputMode: config.outputMode,
                        permissionMode: config.permissionMode,
                    }, createMetadata('execution config', flags));
                }
                else {
                    this.log('');
                    this.log(styles.header('Execution Configuration'));
                    this.log('═'.repeat(50));
                    this.log('');
                    this.log(styles.emphasis('Environment'));
                    this.log(`  defaultEnvironment: ${config.defaultEnvironment}`);
                    this.log(`  defaultExecutor:    ${config.defaultExecutor}`);
                    this.log('');
                    this.log(styles.emphasis('Output'));
                    this.log(`  outputMode:         ${config.outputMode}`);
                    this.log(`  permissionMode:     ${config.permissionMode}`);
                    this.log('');
                    this.log(styles.emphasis('Terminal'));
                    this.log(`  app:                ${config.terminal.app}`);
                    this.log(`  openInBackground:   ${config.terminal.openInBackground}`);
                    this.log('');
                    this.log(styles.emphasis('Shell'));
                    this.log(`  shell:              ${config.shell}`);
                    this.log('');
                    this.log(styles.emphasis('Tmux'));
                    this.log(`  controlMode:        ${config.tmux.controlMode}`);
                    this.log('');
                }
                db.close();
                return;
            }
            // Handle --setting flag (navigate directly to a sub-prompt)
            if (flags.setting) {
                await this.handleSettingPrompt(db, config, flags.setting, jsonModeConfig);
                db.close();
                return;
            }
            // Interactive menu
            const settingChoices = [
                { name: `Default Environment: ${config.defaultEnvironment}`, value: 'defaultEnvironment', command: 'prlt execution config --setting defaultEnvironment --json' },
                { name: `Output Mode: ${config.outputMode}`, value: 'outputMode', command: 'prlt execution config --setting outputMode --json' },
                { name: `Permission Mode: ${config.permissionMode}`, value: 'permissionMode', command: 'prlt execution config --setting permissionMode --json' },
                { name: `Terminal App: ${config.terminal.app}`, value: 'terminal.app', command: 'prlt execution config --setting terminal.app --json' },
                { name: `Open Tabs in Background: ${config.terminal.openInBackground}`, value: 'terminal.openInBackground', command: 'prlt execution config --setting terminal.openInBackground --json' },
                { name: `Shell: ${config.shell}`, value: 'shell', command: 'prlt execution config --setting shell --json' },
                { name: `Tmux Control Mode: ${config.tmux.controlMode}`, value: 'tmux.controlMode', command: 'prlt execution config --setting tmux.controlMode --json' },
            ];
            const { setting } = await this.prompt([
                {
                    type: 'list',
                    name: 'setting',
                    message: 'Select setting to configure:',
                    choices: [
                        new inquirer.Separator('── Execution ──'),
                        ...settingChoices.slice(0, 3),
                        new inquirer.Separator('── Terminal ──'),
                        ...settingChoices.slice(3, 5),
                        new inquirer.Separator('── Shell ──'),
                        settingChoices[5],
                        new inquirer.Separator('── Tmux ──'),
                        settingChoices[6],
                        new inquirer.Separator(),
                        { name: 'Exit', value: '__exit__' },
                    ],
                },
            ], jsonModeConfig);
            if (setting === '__exit__') {
                db.close();
                return;
            }
            // Handle the selected setting
            await this.handleSettingPrompt(db, config, setting, jsonModeConfig);
            db.close();
        }
        catch (error) {
            db.close();
            throw error;
        }
    }
    /**
     * Handle a specific setting's sub-prompt
     */
    async handleSettingPrompt(db, config, setting, jsonModeConfig) {
        switch (setting) {
            case 'defaultEnvironment': {
                const envChoices = [
                    { name: 'host - Run directly on host machine', value: 'host', command: 'prlt execution config --set "defaultEnvironment host" --json' },
                    { name: 'devcontainer - Run in a devcontainer (isolated)', value: 'devcontainer', command: 'prlt execution config --set "defaultEnvironment devcontainer" --json' },
                    { name: 'docker - Run in a Docker container', value: 'docker', command: 'prlt execution config --set "defaultEnvironment docker" --json' },
                    { name: 'vm - Run on a remote VM', value: 'vm', command: 'prlt execution config --set "defaultEnvironment vm" --json' },
                ];
                const { newEnv } = await this.prompt([
                    {
                        type: 'list',
                        name: 'newEnv',
                        message: 'Select default execution environment:',
                        choices: envChoices,
                        default: config.defaultEnvironment,
                    },
                ], jsonModeConfig);
                saveExecutionSetting(db, 'defaultMode', newEnv);
                this.log(styles.success(`Default environment set to: ${newEnv}`));
                break;
            }
            case 'outputMode': {
                const outputChoices = [
                    { name: 'interactive - Watch Claude work in real-time (streaming UI)', value: 'interactive', command: 'prlt execution config --set "outputMode interactive" --json' },
                    { name: 'print - Show final result only (better for logs)', value: 'print', command: 'prlt execution config --set "outputMode print" --json' },
                ];
                const { newOutput } = await this.prompt([
                    {
                        type: 'list',
                        name: 'newOutput',
                        message: 'Select output mode:',
                        choices: outputChoices,
                        default: config.outputMode,
                    },
                ], jsonModeConfig);
                this.setConfigValue(db, 'outputMode', newOutput, false);
                this.log(styles.success(`Output mode set to: ${newOutput}`));
                break;
            }
            case 'permissionMode': {
                const permChoices = [
                    { name: '🔒 safe   - Requires approval for dangerous operations (recommended)', value: 'safe', command: 'prlt execution config --set "permissionMode safe" --json' },
                    { name: '⚠️  danger - Skip permission checks (--dangerously-skip-permissions)', value: 'danger', command: 'prlt execution config --set "permissionMode danger" --json' },
                ];
                const { newPerm } = await this.prompt([
                    {
                        type: 'list',
                        name: 'newPerm',
                        message: 'Select permission mode:',
                        choices: permChoices,
                        default: config.permissionMode,
                    },
                ], jsonModeConfig);
                this.setConfigValue(db, 'permissionMode', newPerm, false);
                this.log(styles.success(`Permission mode set to: ${newPerm}`));
                break;
            }
            case 'terminal.app': {
                const appChoices = [
                    { name: 'iTerm', value: 'iTerm', command: 'prlt execution config --set "terminal.app iTerm" --json' },
                    { name: 'Terminal.app (macOS default)', value: 'Terminal', command: 'prlt execution config --set "terminal.app Terminal" --json' },
                    { name: 'Ghostty', value: 'Ghostty', command: 'prlt execution config --set "terminal.app Ghostty" --json' },
                    { name: 'Alacritty', value: 'Alacritty', command: 'prlt execution config --set "terminal.app Alacritty" --json' },
                    { name: 'Kitty', value: 'Kitty', command: 'prlt execution config --set "terminal.app Kitty" --json' },
                    { name: 'WezTerm', value: 'WezTerm', command: 'prlt execution config --set "terminal.app WezTerm" --json' },
                    { name: 'Warp', value: 'Warp', command: 'prlt execution config --set "terminal.app Warp" --json' },
                    { name: 'tmux', value: 'tmux', command: 'prlt execution config --set "terminal.app tmux" --json' },
                ];
                const { newApp } = await this.prompt([
                    {
                        type: 'list',
                        name: 'newApp',
                        message: 'Select terminal app:',
                        choices: appChoices,
                        default: config.terminal.app,
                    },
                ], jsonModeConfig);
                saveTerminalApp(db, newApp);
                this.log(styles.success(`Terminal app set to: ${newApp}`));
                break;
            }
            case 'terminal.openInBackground': {
                const bgChoices = [
                    { name: 'Yes - Open tabs in background (don\'t steal focus)', value: 'true', command: 'prlt execution config --set "terminal.openInBackground true" --json' },
                    { name: 'No - Bring terminal to foreground when opening tabs', value: 'false', command: 'prlt execution config --set "terminal.openInBackground false" --json' },
                ];
                const { openInBg } = await this.prompt([
                    {
                        type: 'list',
                        name: 'openInBg',
                        message: 'Open terminal tabs in background?',
                        choices: bgChoices,
                        default: String(config.terminal.openInBackground),
                    },
                ], jsonModeConfig);
                saveTerminalOpenInBackground(db, openInBg === 'true');
                this.log(styles.success(`Open in background set to: ${openInBg}`));
                break;
            }
            case 'shell': {
                const shellChoices = [
                    { name: 'zsh (macOS default)', value: 'zsh', command: 'prlt execution config --set "shell zsh" --json' },
                    { name: 'bash', value: 'bash', command: 'prlt execution config --set "shell bash" --json' },
                    { name: 'fish', value: 'fish', command: 'prlt execution config --set "shell fish" --json' },
                ];
                const { newShell } = await this.prompt([
                    {
                        type: 'list',
                        name: 'newShell',
                        message: 'Select shell:',
                        choices: shellChoices,
                        default: config.shell,
                    },
                ], jsonModeConfig);
                saveShell(db, newShell);
                this.log(styles.success(`Shell set to: ${newShell}`));
                break;
            }
            case 'tmux.controlMode': {
                const ccChoices = [
                    { name: 'Yes - Use tmux -CC for native iTerm integration', value: 'true', command: 'prlt execution config --set "tmux.controlMode true" --json' },
                    { name: 'No - Standard tmux interface', value: 'false', command: 'prlt execution config --set "tmux.controlMode false" --json' },
                ];
                const { controlMode } = await this.prompt([
                    {
                        type: 'list',
                        name: 'controlMode',
                        message: 'Enable tmux control mode (-CC)?',
                        choices: ccChoices,
                        default: String(config.tmux.controlMode),
                    },
                ], jsonModeConfig);
                saveTmuxControlMode(db, controlMode === 'true');
                this.log(styles.success(`Tmux control mode set to: ${controlMode}`));
                break;
            }
            default: {
                const jsonMode = shouldOutputJson(jsonModeConfig?.flags ?? {});
                if (jsonMode) {
                    outputErrorAsJson('UNKNOWN_SETTING', `Unknown setting: ${setting}`, createMetadata('execution config', jsonModeConfig?.flags ?? {}));
                }
                this.error(`Unknown setting: ${setting}`);
            }
        }
    }
    setConfigValue(db, key, value, jsonMode) {
        const normalizedKey = key.toLowerCase();
        // Define valid values for each config key
        const VALID_VALUES = {
            defaultenvironment: ['host', 'devcontainer', 'docker', 'vm'],
            outputmode: ['interactive', 'print'],
            permissionmode: ['safe', 'danger'],
            'terminal.app': ['Terminal', 'iTerm', 'Alacritty', 'Ghostty', 'Kitty', 'tmux', 'Warp', 'WezTerm'],
            'terminal.openinbackground': ['true', 'false'],
            shell: ['bash', 'zsh', 'fish'],
            'tmux.controlmode': ['true', 'false'],
        };
        // Validate value against allowed options
        const validValues = VALID_VALUES[normalizedKey];
        if (validValues && !validValues.includes(value)) {
            const errorMsg = `Invalid value "${value}" for ${key}. Valid options: ${validValues.join(', ')}`;
            if (jsonMode) {
                outputErrorAsJson('INVALID_VALUE', errorMsg, createMetadata('execution config', {}));
            }
            else {
                this.error(errorMsg);
            }
            return;
        }
        switch (normalizedKey) {
            case 'defaultenvironment':
                saveExecutionSetting(db, 'defaultMode', value);
                break;
            case 'outputmode':
                // Store output mode - need to add this to the config storage
                this.setOutputMode(db, value);
                break;
            case 'permissionmode':
                // Store permission mode preference
                this.setPermissionMode(db, value);
                break;
            case 'terminal.app':
                saveTerminalApp(db, value);
                break;
            case 'terminal.openinbackground':
                saveTerminalOpenInBackground(db, value.toLowerCase() === 'true');
                break;
            case 'shell':
                saveShell(db, value);
                break;
            case 'tmux.controlmode':
                saveTmuxControlMode(db, value.toLowerCase() === 'true');
                break;
            default:
                if (jsonMode) {
                    outputErrorAsJson('UNKNOWN_KEY', `Unknown config key: ${key}`, createMetadata('execution config', {}));
                }
                else {
                    this.warn(`Unknown config key: ${key}`);
                }
                return;
        }
        if (jsonMode) {
            outputSuccessAsJson({ key, value }, createMetadata('execution config', {}));
        }
        else {
            this.log(styles.success(`Set ${key} = ${value}`));
        }
    }
    /**
     * Save output mode to workspace settings
     */
    setOutputMode(db, mode) {
        saveExecutionSetting(db, 'outputMode', mode);
    }
    /**
     * Save permission mode preference to workspace settings
     */
    setPermissionMode(db, mode) {
        saveExecutionSetting(db, 'permissionMode', mode);
    }
}
//# sourceMappingURL=config.js.map