import { BadRequestException, Body, Controller, Get, Post, Query, Render } from '@nestjs/common';
import { RecordService } from './record.service';

@Controller('record')
export class RecordController {
  constructor(private readonly recordService: RecordService) { }

  @Post('/create')
  async createRecord(
    @Body() body: { agency: string; name: string },
  ) {
    const { agency, name } = body;

    if (!agency) {
      throw new BadRequestException('agency is required');
    }

    const record = await this.recordService.createRecord(agency, name);

    return {
      message: 'Record created successfully',
      data: record,
    };
  }

  @Post("create-manual")
  create(@Body("agency") agency: string, @Body("name") name: string) {
    return this.recordService.createRecord(agency || "manual", name || "manual");
  }

  @Post("create-all")
  createAll() {
    return this.recordService.createAllToMax();
  }

  @Get('/count')
  async countRecords() {
    const count = await this.recordService.countRecords();
    return {
      total: count,
    };
  }

  @Get('/total')
  @Render('record-summary')
  async getRecordSummary() {
    const total = await this.recordService.countRecords();
    return { total };
  }

}
