/* eslint-disable n/no-process-exit */
/* eslint-disable unicorn/no-process-exit */
/* eslint-disable perfectionist/sort-objects */
/* eslint-disable perfectionist/sort-classes */
/* eslint-disable perfectionist/sort-named-imports */
/* eslint-disable perfectionist/sort-imports */
import { Command } from '@oclif/core';
import prompts from '../prompts/index.js';
import api from '../aws/index.js';
import Conf from 'conf';
import log from '../util/log.js';
const config = new Conf({ projectName: "campfire" });
const clientsObj = api.clients;
import { populateWorkflows, createWorkflowDir, copyWorkflowFilesToRepo, } from '../util/fs.js';
const getAppInfo = async () => {
    const appInfo = await prompts.appInfoPrompt();
    return appInfo;
};
const addAppToConfigFile = (APP_NAME) => {
    if (config.get('APP_NAMES') === undefined) {
        log.warn('Oops! Something went wrong T^T \nPlease run campfire destroy and try again.');
        process.exit(0);
    }
    const apps = JSON.parse(config.get('APP_NAMES'));
    apps.push(APP_NAME);
    config.set('APP_NAMES', JSON.stringify(apps));
};
export default class Init extends Command {
    static description = 'Initialize cluster for a new Campfire deploy preview environment';
    async run() {
        await log.header('Hey, camper! Time to get Campfire initialized in your project repository:\n');
        createWorkflowDir();
        copyWorkflowFilesToRepo();
        const appInfo = await getAppInfo();
        addAppToConfigFile(appInfo.APP_NAME);
        config.set("CLUSTER_NAME", appInfo.APP_NAME);
        await log.header('\nCreate these two GitHub Secrets in your repository:');
        process.stdout.write("AWS_ACCESS_KEY_ID      ");
        log.info(config.get("AWS_ACCESS_KEY_ID"));
        process.stdout.write("AWS_SECRET_ACCESS_KEY  ");
        log.info(config.get("AWS_SECRET_ACCESS_KEY"));
        await log.text('   ');
        clientsObj.ecs = await api.initializeEcsClient(config.get('AWS_REGION'));
        await api.createCluster({ clusterName: appInfo.APP_NAME });
        log.note("NOTE: Don't forget to install the Campfire GitHub App on your repository before making any pull requests!");
        populateWorkflows();
    }
}
