'use strict';

const tools = require('../../utils/tools');
const configUtils = require('../../csdr/config/config-utils');

// inner modules
const innerInitProject = require('./init-project');
const innerInitProjectsGroup = require('./init-projects-group');
const innerInitPackage = require('./init-package');
const innerInitRemote = require('./init-remote');
const innerInitCustom = require('./init-custom');


// Getting arguments if they are provided for the CI/CD pipeline
const {
  project, team, pkg, branch, configOnly, skipClone, skipInstall, reset, pkgOnly, prjOnly,
  build, containerOnly, skipAppContainers, remote, euiVersion, remoteOnly, virtual, custom,
  projectsGroup,
} = tools.getArgs();


module.exports.init = () => {

  // check known init types
  if (!project && !pkg && !remote && !custom && !projectsGroup ) {
    tools.logError('****************************************');
    tools.logError('*************** ERROR ******************');
    tools.logError('****************************************\n');
    tools.logError('At least "pkg" or "project" or "remote" type should be used for init, for example :\n');
    tools.logError('npm run init -- --pkg PKG_NAME [--branch BRANCH_NAME|default develop] [--git local] [--pkgOnly] [--skipInstall]');
    tools.logError('npm run init -- --project PROJECT_NAME [--branch BRANCH_NAME|default develop] [--git local] [--prjOnly] [--skipInstall]');
    tools.logError('npm run init -- --remote VIRTUAL_REMOTE_NAME [--remoteOnly] [--skipInstall]');
    tools.logError('npm run init -- --custom [--skipInstall] [--git local]');
    process.exit(0);
  }

  return Promise.resolve()
    // check if csdr default root, if not cloning specific config repo (CSDR v2 2024)
    .then(() => {
      if (!configUtils.global.isCsdr()) {
        return configUtils.global.cloneConfig();
      }
    })

    .then(() => {
      // Initializing a common response
      const initialResponse = {
        project: project || null,
        team: team || 'all',
        pkg: pkg || null,
        remote: remote || null,
        virtual: virtual || false,
        euiVersion: euiVersion || null,
        branch: branch || 'develop',
        configOnly: configOnly || false,
        skipClone: skipClone || false,
        reset: reset || false,
        pkgOnly: pkgOnly || false,
        prjOnly: prjOnly || false,
        remoteOnly: remoteOnly || false,
        build: build || false,
        containerOnly: containerOnly || false,
        skipAppContainers: skipAppContainers || false,
        custom: custom || false,
        projectsGroup: projectsGroup || null,
      }

      // converting inputs args
      initialResponse.branch = tools.convertStringArg(initialResponse.branch, 'develop');
      initialResponse.euiVersion = tools.convertStringArg(initialResponse.euiVersion);

      // setting project to null if forced to NONE
      if (initialResponse.project === 'NONE') {
        initialResponse.project = null;
      }

      // checking if provided PKG (from gitlab) is a virtual remote (new remote kind as of v15 (and sedia v10))
      const isVirtualRemote = configUtils.remotes.isVirtualRemote(initialResponse.pkg);
      if (isVirtualRemote) {
        initialResponse.remote = initialResponse.pkg;
        initialResponse.virtual = true;
        initialResponse.pkg = null;
      }

      tools.logInfo('Configuration generated based on provided args:');
      console.log(initialResponse);

      if (initialResponse.project) {
        return innerInitProject.init(initialResponse);

      } else if (initialResponse.pkg) {
        return innerInitPackage.init(initialResponse);

      } else if (initialResponse.remote) {
        return innerInitRemote.init(initialResponse);

      } else if (initialResponse.custom) {
        return innerInitCustom.init(initialResponse);

      } else if (initialResponse.projectsGroup) {
        return innerInitProjectsGroup.init(initialResponse);
      }
    })

    .catch((e) => {
      throw e;
    });
}
