const path = require('path');
const fs = require('fs');

module.exports = function (plop) {
  plop.setGenerator('module', {
    description: 'Create a new module by default',
    prompts: [
      {
        type: 'input',
        name: 'name',
        message: 'Name: ',
        validate: function (value) {
          if (/.+/.test(value)) {
            return true;
          }
          return 'Name is required';
        },
      },
      {
        type: 'list',
        name: 'base',
        message: 'Base: ',
        choices: function () {
          return ['base', 'base status', 'base core'];
        },
      },
      {
        type: 'list',
        name: 'location',
        message: 'Location: ',
        choices: function () {
          return ['item related', 'user related', 'season related', 'transaction', 'web information'];
        },
      },
    ],

    actions: function (data) {
      if (['base', 'base core'].includes(data.base)) data.orchestrator = 'data'
      else if (data.base == 'base status') data.orchestrator = 'data transaction'

      const destination = `src/modules/{{dashCase location}}/{{dashCase name}}`;

      const result = [
        ...mappingModule(data.base, destination),
        ...mappingController(data.base, destination),
        ...mappingModel(data.base, destination),
        ...mappingService(destination),
        ...mappingOrchestrator(data.base, destination),
        ...mappingManager(data.base, destination)
      ]

      return result
    },
  })
};

function mappingService(destination) {
  const datas = [];

  datas.push(
    {
      type: 'addMany',
      destination: `${destination}/data/services`,
      templateFiles: `src/core/templates/services/*.hbs`,
      base: 'src/core/templates/services',
    },
  )

  return datas;
}

function mappingOrchestrator(base, destination) {
  const datas = [];

  if (base == 'base status') {
    datas.push(
      {
        type: 'addMany',
        destination: `${destination}/domain/usecases`,
        templateFiles: `src/core/templates/orchestrators/base-status/*.hbs`,
        base: 'src/core/templates/orchestrators/base-status',
      },
    )
  } else {
    datas.push(
      {
        type: 'addMany',
        destination: `${destination}/domain/usecases`,
        templateFiles: `src/core/templates/orchestrators/base/*.hbs`,
        base: 'src/core/templates/orchestrators/base',
      },
    )
  }

  datas.push(
    {
      type: 'addMany',
      destination: `${destination}/domain/usecases`,
      templateFiles: `src/core/templates/orchestrators/base-read/*.hbs`,
      base: 'src/core/templates/orchestrators/base-read',
    },
  )

  return datas;
}

function mappingController(base, destination) {
  const datas = [];

  if (base == 'base status') {
    datas.push(
      {
        type: 'addMany',
        destination: `${destination}/infrastructure`,
        templateFiles: `src/core/templates/controllers/base-status/*.hbs`,
        base: 'src/core/templates/controllers/base-status',
      },
    )
  } else {
    datas.push(
      {
        type: 'addMany',
        destination: `${destination}/infrastructure`,
        templateFiles: `src/core/templates/controllers/base/*.hbs`,
        base: 'src/core/templates/controllers/base',
      },
    )
  }

  datas.push(
    {
      type: 'addMany',
      destination: `${destination}/infrastructure`,
      templateFiles: `src/core/templates/controllers/base-read/*.hbs`,
      base: 'src/core/templates/controllers/base-read',
    },
  )

  return datas;
}

function mappingModel(base, destination) {
  const datas = [];

  if (base == 'base status') {
    datas.push(
      {
        type: 'addMany',
        destination: `${destination}/domain/entities/event`,
        templateFiles: `src/core/templates/events/base-status/*.hbs`,
        base: 'src/core/templates/events/base-status',
      }
    )
  }
  
  datas.push(
    {
      type: 'addMany',
      destination: `${destination}/data/models`,
      templateFiles: `src/core/templates/models/*.hbs`,
      base: 'src/core/templates/models',
    },
    {
      type: 'addMany',
      destination: `${destination}/domain/entities`,
      templateFiles: `src/core/templates/entities/*.hbs`,
      base: 'src/core/templates/entities',
    },
    {
      type: 'addMany',
      destination: `${destination}/infrastructure/dto`,
      templateFiles: `src/core/templates/dtos/*.hbs`,
      base: 'src/core/templates/dtos',
    },
    {
      type: 'addMany',
      destination: `${destination}/domain/entities/event`,
      templateFiles: `src/core/templates/events/base/*.hbs`,
      base: 'src/core/templates/events/base',
    }
  )

  return datas;
}

function mappingModule(base, destination) {
  const datas = [];

  if (base == 'base status') {
    datas.push(
      {
        type: 'addMany',
        destination: destination,
        templateFiles: `src/core/templates/modules/base-status/*.hbs`,
        base: 'src/core/templates/modules/base-status',
      }
    )
  } else {
    datas.push(
      {
        type: 'addMany',
        destination: destination,
        templateFiles: `src/core/templates/modules/base/*.hbs`,
        base: 'src/core/templates/modules/base',
      }
    )
  }

  datas.push(
    {
      type: 'addMany',
      destination: destination,
      templateFiles: `src/core/templates/modules/core/*.hbs`,
      base: 'src/core/templates/modules/core',
    }
  )

  return datas;
}

function mappingManager(base, destination) {
  const datas = [];

  const tujuan = `${destination}/domain/usecases/managers`;
  if (base == 'base status') {
    datas.push(
      {
        type: 'addMany',
        destination: tujuan,
        templateFiles: `src/core/templates/managers/manager-statuses/*.hbs`,
        base: 'src/core/templates/managers/manager-statuses',
      },
    )
  }

  datas.push(
    {
      type: 'addMany',
      destination: tujuan,
      templateFiles: `src/core/templates/managers/base/*.hbs`,
      base: 'src/core/templates/managers/base',
    },
  )

  return datas;
}