import { Controller, Get, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { EccmConfig } from 'src/models/eccm-config.model';
import {
  CurrentIssuesEccmReport,
  CurrentIssuesEccmReportService,
} from './current-issues-eccm.report.service';

@Controller('current-issues-eccm')
export class CurrentIssuesEccmReportController {
  private logger = new Logger(CurrentIssuesEccmReportController.name);
  private eccmConfig: EccmConfig.Config;

  /* eslint-disable */
  private reportTemplate = [
    // context - this.report: UserReport[]
    '{{#each this}}',
      '<div>',
        // context - UserReport
        '<p>{{user.firstname}} {{user.lastname}}:</p>',
        '<ul>',
          '{{#each issuesGroupedByStatus}}',
            '<li>',
              // context - IssuesAndStatus
              '{{status.name}}:',
              '<ul>',
                '{{#each issues}}',
                  '<li>',
                    // context - RedmineTypes.Issue
                    `{{>redmineIssueAHref issue=.}}: {{subject}} (прио - {{priority.name}}, версия - {{fixed_version.name}})`,
                  '</li>',
                '{{/each}}',
              '</ul>',
            '</li>',
          '{{/each}}',
        '</ul>',
      '</div>',
    '{{/each}}',
  ].join('\n');
  /* eslint-enable */

  constructor(
    private currentIssuesEccmReportService: CurrentIssuesEccmReportService,
    private configService: ConfigService,
  ) {
    this.eccmConfig = this.configService.get<EccmConfig.Config>('redmineEccm');
    this.logger.debug(`Eccm config - ${JSON.stringify(this.eccmConfig)}`);
  }

  @Get()
  async getReport(): Promise<string> {
    return await this.currentIssuesEccmReportService.getReport({
      project: this.eccmConfig.projectName,
      versions: this.eccmConfig.currentVersions,
      statuses: this.eccmConfig.currentIssuesStatuses,
      template: this.reportTemplate,
      fields: CurrentIssuesEccmReport.Defaults.currentIssuesFields,
    });
  }
}
