import { DynamicLoader } from '@app/event-emitter/configs/dynamic-loader';
import { Controller, Get, Param, Render } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { IssuesByTagsWidgetDataLoaderService } from './widget-data-loader/issues-by-tags.widget-data-loader.service';
import { parse } from 'jsonc-parser';

@Controller('simple-issues-list')
export class SimpleIssuesListController {
  private path: string;

  constructor(
    private issuesByTagsWidgetDataLoaderService: IssuesByTagsWidgetDataLoaderService,
    private dynamicLoader: DynamicLoader,
    private configService: ConfigService,
  ) {
    this.path = this.configService.get<string>('simpleKanbanBoard.path');
  }

  @Get('/by-tags/:name/raw')
  async getByTagsRawData(@Param('name') name: string): Promise<any> {
    const cfg = this.dynamicLoader.load(name, {
      path: this.path,
      ext: 'jsonc',
      parser: parse,
    });
    return await this.issuesByTagsWidgetDataLoaderService.load(cfg);
  }

  @Get('/by-tags/:name')
  @Render('simple-issues-list')
  async getByTags(@Param('name') name: string): Promise<any> {
    return await this.getByTagsRawData(name);
  }
}
