import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Query,
  Res,
} from "@nestjs/common"
import { CreatePaymentDto } from "../dto/create-payment.dto"
import { UpdatePaymentDto } from "../dto/update-payment.dto"
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger"
import { AuthGuardMiddleware } from "src/middleware/auth-guard.middleware"
import { FilterPaymentDto } from "../dto/filter-payment.dto"
import { PaymentService } from "./payment.service"
import { ExportExcel } from "src/common/decorators/excel-decorator"
import { Response } from "express"

@ApiTags("Payments")
@Controller("payments")
export class PaymentController {
  constructor(private readonly paymentsService: PaymentService) {}

  @Post()
  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  create(@Body() createPaymentDto: CreatePaymentDto) {
    return this.paymentsService.create(createPaymentDto)
  }

  @Get()
  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  findAll(@Query() filterPaymentDto: FilterPaymentDto) {
    return this.paymentsService.findAll(filterPaymentDto)
  }

  @Get("excel/export")
  @ExportExcel("Payment-excel-export")
  async paymentExcel(
    @Query() filterPaymentDto: FilterPaymentDto,
    @Res() res: Response,
  ) {
    const buffer = await this.paymentsService.excelExport(filterPaymentDto)
    res.end(buffer)
  }

  @Get(":id")
  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  findOne(@Param("id") id: string) {
    return this.paymentsService.findOne(+id)
  }

  @Patch(":id")
  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  update(@Param("id") id: string, @Body() updatePaymentDto: UpdatePaymentDto) {
    return this.paymentsService.update(+id, updatePaymentDto)
  }

  @Delete(":id")
  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  remove(@Param("id") id: string) {
    return this.paymentsService.remove(+id)
  }
}
