/**
 * make:notification-template
 * Scaffolds a notification markdown template into the project.
 */
import { BaseCommand } from '../BaseCommand.js';
import { ErrorHandler } from '../ErrorHandler.js';
import { PromptHelper } from '../PromptHelper.js';
import { FileGenerator } from '../scaffolding/FileGenerator.js';
import { TemplateGenerator } from '../scaffolding/TemplateGenerator.js';
import { Env } from '../../config/env.js';
import { ErrorFactory } from '../../exceptions/ZintrustError.js';
import * as path from '../../node-singletons/path.js';
import inquirer from 'inquirer';
const addOptions = (command) => {
    command.argument('[name]', 'Template name (alphanumeric + hyphens only)');
    command.option('--channels <csv>', 'Comma-separated channels: mail,sms,slack,discord');
    command.option('--vars <csv>', 'Comma-separated variables');
    command.option('--sms-variant <mode>', 'short|none', 'none');
    command.option('--overwrite', 'Overwrite existing file');
    command.option('--no-interactive', 'Disable prompts (requires args/options)');
};
const defaultCopyright = () => Env.TEMPLATE_COPYRIGHT;
const parseChannelsCsv = (csv) => {
    if (csv === undefined || csv.trim() === '')
        return [];
    const parts = csv
        .split(',')
        .map((p) => p.trim())
        .filter((p) => p !== '');
    const out = [];
    for (const p of parts) {
        const lower = p.toLowerCase();
        if (lower === 'mail' || lower === 'sms' || lower === 'slack' || lower === 'discord') {
            if (!out.includes(lower))
                out.push(lower);
        }
    }
    return out;
};
const resolveName = async (options, interactive) => {
    const argName = options.args?.[0];
    const name = argName ??
        (interactive ? await PromptHelper.textInput('Template name:', 'security-alert', true) : '');
    if (name.trim() === '') {
        throw ErrorFactory.createValidationError('Template name required');
    }
    return name;
};
const resolveChannels = async (options, interactive) => {
    let channels = parseChannelsCsv(options.channels);
    if (channels.length > 0)
        return channels;
    if (!interactive)
        return [];
    const answer = await inquirer.prompt([
        {
            type: 'checkbox',
            name: 'channels',
            message: 'Channels:',
            choices: ['mail', 'sms', 'slack', 'discord'],
            default: ['mail'],
        },
    ]);
    channels = answer.channels ?? ['mail'];
    return channels;
};
const resolveVariables = async (options, interactive) => {
    const varsCsv = options.vars ??
        (interactive
            ? await PromptHelper.textInput('Variables (comma-separated):', 'ipAddress,location,deviceName,reviewLink', true)
            : '');
    return TemplateGenerator.parseVariablesCsv(varsCsv);
};
const maybeEnableOverwrite = async (interactive, overwriteFlag, relPath) => {
    if (!interactive || overwriteFlag)
        return overwriteFlag;
    const fullPath = path.join(process.cwd(), relPath);
    if (!FileGenerator.fileExists(fullPath))
        return false;
    return PromptHelper.confirm('File exists. Overwrite?', false, true);
};
export const MakeNotificationTemplateCommand = Object.freeze({
    create() {
        return BaseCommand.create({
            name: 'make:notification-template',
            aliases: 'make:notification',
            description: 'Scaffold a notification markdown template into src/notification/markdown',
            addOptions,
            execute: async (options) => {
                const interactive = options.noInteractive !== true;
                const name = await resolveName(options, interactive);
                const channels = await resolveChannels(options, interactive);
                const variables = await resolveVariables(options, interactive);
                const smsVariant = (options.smsVariant ?? 'none');
                TemplateGenerator.ensureDirectories(process.cwd());
                const relPath = path.join('src', 'notification', 'markdown', `${name}.md`);
                const overwrite = await maybeEnableOverwrite(interactive, options.overwrite === true, relPath);
                const result = TemplateGenerator.scaffoldNotificationMarkdownTemplate({
                    name,
                    channels,
                    variables,
                    smsVariant,
                    copyright: defaultCopyright(),
                    projectRoot: process.cwd(),
                    overwrite,
                });
                if (result.success)
                    ErrorHandler.success(result.message);
                else
                    ErrorHandler.warn(result.message);
            },
        });
    },
});
export default MakeNotificationTemplateCommand;
