import { Controller, Get, Param, Query, Render } from '@nestjs/common';
import { DailyEccmWithExtraDataService } from './daily-eccm-with-extra-data.service';
import {
  DailyEccmReport,
  DailyEccmReportService,
} from './daily-eccm.report.service';

@Controller('daily-eccm')
export class DailyEccmReportController {
  constructor(
    private dailyEccmReportService: DailyEccmReportService,
    private dailyEccmWithExtraDataService: DailyEccmWithExtraDataService,
  ) {}

  @Get()
  @Render('daily-eccm-report')
  async getReportDefault(
    @Query('from') from: string,
    @Query('to') to: string,
  ): Promise<DailyEccmReport.Models.Report> {
    return await this.getReport(from, to);
  }

  @Get('/delete/:name')
  async deleteReport(@Param('name') name: string): Promise<boolean> {
    return await this.dailyEccmReportService.deleteReport(name);
  }

  @Get('/load/:name')
  @Render('daily-eccm-report')
  async loadReport(
    @Param('name') name: string,
  ): Promise<DailyEccmReport.Models.Report> {
    return await this.dailyEccmReportService.loadReport(name);
  }

  @Get('/load/:name/raw')
  async loadReportRawData(
    @Param('name') name: string,
  ): Promise<DailyEccmReport.Models.Report> {
    return await this.dailyEccmReportService.loadReport(name);
  }

  @Get('/generate')
  @Render('daily-eccm-report')
  async getReport(
    @Query('from') from: string,
    @Query('to') to: string,
    @Query('name') name?: string,
    @Query('overwrite') overwrite?: boolean,
  ): Promise<DailyEccmReport.Models.Report> {
    return await this.getReportRawData(from, to, name, overwrite);
  }

  @Get('/generate/raw')
  async getReportRawData(
    @Query('from') from: string,
    @Query('to') to: string,
    @Query('name') name?: string,
    @Query('overwrite') overwrite?: boolean,
  ): Promise<DailyEccmReport.Models.Report> {
    const params = this.dailyEccmReportService.generateParams(from, to, name);
    const data = await this.dailyEccmReportService.generateReport(params);
    if (name) {
      const saveResult = await this.dailyEccmReportService.saveReport(
        data,
        overwrite,
      );
      return saveResult ? data : null;
    } else {
      return data;
    }
  }

  @Get('/load/:name/extended/raw')
  async loadExtendedReportRawData(
    @Param('name') name: string,
  ): Promise<DailyEccmReport.Models.Report> {
    return await this.dailyEccmWithExtraDataService.loadReport(name);
  }

  @Get('/load/:name/extended')
  @Render('daily-eccm-report-extended')
  async loadExtendedReport(
    @Param('name') name: string,
  ): Promise<DailyEccmReport.Models.Report> {
    return await this.dailyEccmWithExtraDataService.loadReport(name);
  }
}
