// generate-modules-with-services-folder.js
const fs = require('fs');
const path = require('path');

const modules = [
  { name: 'transactions', children: ['list', 'filters', 'details', 'export'] },
  { name: 'merchants', children: ['list', 'config', 'history'] },
  { name: 'operators', children: ['config', 'stats'] },
  { name: 'notifications', children: ['list', 'filters', 'actions'] },
  { name: 'webhooks', children: ['history', 'status', 'retry'] },
  { name: 'users', children: ['list', 'roles'] },
  { name: 'settings', children: [] },
  { name: 'integrations', children: [] },
  { name: 'support', children: [] },
  { name: 'profile', children: [] },
  { name: 'documentation', children: [] },
  { name: 'help', children: [] },
  { name: 'about', children: [] },
];

const baseDir = path.join(__dirname, 'src/app/modules');

function createModule(module) {
  const modulePath = path.join(baseDir, module.name);
  fs.mkdirSync(modulePath, { recursive: true });

  // components folder
  fs.mkdirSync(path.join(modulePath, 'components'), { recursive: true });

  // services folder inside module
  const serviceFolder = path.join(modulePath, 'services');
  fs.mkdirSync(serviceFolder, { recursive: true });

  // main module files
  fs.writeFileSync(path.join(modulePath, `${module.name}.ts`), `
import { Component } from '@angular/core';

@Component({
  selector: 'app-${module.name}',
  templateUrl: './${module.name}.html',
})
export class ${capitalize(module.name)} {}
  `.trim());

  fs.writeFileSync(path.join(modulePath, `${module.name}.html`), `<p>${capitalize(module.name)}</p>`);
  fs.writeFileSync(path.join(modulePath, `${module.name}.spec.ts`), `
import { ${capitalize(module.name)} } from './${module.name}';
describe('${capitalize(module.name)}', () => {});
  `.trim());

  // service for module
  fs.writeFileSync(path.join(serviceFolder, `${module.name}.service.ts`), `
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class ${capitalize(module.name)}Service {
  constructor() {}
}
  `.trim());

  // children
  module.children.forEach(child => {
    const childPath = path.join(modulePath, child);
    fs.mkdirSync(childPath, { recursive: true });

    fs.writeFileSync(path.join(childPath, `${child}.ts`), `
import { Component } from '@angular/core';

@Component({
  selector: 'app-${module.name}-${child}',
  templateUrl: './${child}.html',
})
export class ${capitalize(module.name)}${capitalize(child)} {}
    `.trim());

    fs.writeFileSync(path.join(childPath, `${child}.html`), `<p>${capitalize(module.name)} - ${capitalize(child)}</p>`);
    fs.writeFileSync(path.join(childPath, `${child}.spec.ts`), `
import { ${capitalize(module.name)}${capitalize(child)} } from './${child}';
describe('${capitalize(module.name)}${capitalize(child)}', () => {});
    `.trim());

    // optional: service for child inside service folder
    fs.writeFileSync(path.join(serviceFolder, `${child}.service.ts`), `
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class ${capitalize(module.name)}${capitalize(child)}Service {
  constructor() {}
}
    `.trim());
  });
}

function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

modules.forEach(createModule);
console.log('Modules and services generated successfully with service folder in each module');
