import { program } from 'commander';
import { GitUtils } from '../utils/git.utils';
import fs from 'fs';

export class MergeRequestTemplate {
  private readonly GITLAB_MR_TEMPLATE_DIR = '.gitlab/merge_request_templates';
  private readonly DEFAULT_MR_TEMPLATE = 'default.md';

  constructor() {}

  setup() {
    this.setupCreateMergeRequestTemplateCommand();
  }

  private setupCreateMergeRequestTemplateCommand() {
    program
      .command('merge-request-template')
      .description('Setup git commit-msg hook for conventional commits')
      .action(async () => {
        this.createMergeRequestTemplate();
      });
  }

  private createMergeRequestTemplate() {
    if (GitUtils.logErrorIfNotInGitProject()) return;

    if (!fs.existsSync(this.GITLAB_MR_TEMPLATE_DIR)) {
      fs.mkdirSync(this.GITLAB_MR_TEMPLATE_DIR, { recursive: true });
    }

    if (!fs.existsSync(`${this.GITLAB_MR_TEMPLATE_DIR}/${this.DEFAULT_MR_TEMPLATE}`)) {
      fs.writeFileSync(
        `${this.GITLAB_MR_TEMPLATE_DIR}/${this.DEFAULT_MR_TEMPLATE}`,
        `### Checklists (The creator will check and the reviewer will review

- [ ] Update Docs
- [ ] Write Tests`,
      );
    }
  }
}
