import { Controller, Get, Post, Body, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { CouponBatchService } from '../../../application/services/coupon-batch.service';
import {
  BatchIssueDto,
  BatchRecallDto,
  BatchPriceAdjustDto,
  ListBatchOperationsQueryDto,
} from '../dto/coupon-batch.dto';

@ApiTags('Coupon Batch Operations')
@Controller('coupons/batch')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class CouponBatchController {
  constructor(private readonly couponBatchService: CouponBatchService) {}

  @Post('issue')
  @ApiOperation({ summary: 'Batch issue coupons from a template' })
  async batchIssue(@Req() req: any, @Body() dto: BatchIssueDto) {
    const result = await this.couponBatchService.batchIssue(
      req.user.issuerId,
      dto.templateId,
      dto.quantity,
      dto.expiryDate,
    );
    return { code: 0, data: result };
  }

  @Post('recall')
  @ApiOperation({ summary: 'Batch recall coupons' })
  async batchRecall(@Req() req: any, @Body() dto: BatchRecallDto) {
    const result = await this.couponBatchService.batchRecall(
      req.user.issuerId,
      dto.couponIds,
      dto.reason,
    );
    return { code: 0, data: result };
  }

  @Post('price-adjust')
  @ApiOperation({ summary: 'Batch adjust coupon prices' })
  async batchPriceAdjust(@Req() req: any, @Body() dto: BatchPriceAdjustDto) {
    const result = await this.couponBatchService.batchPriceAdjust(
      req.user.issuerId,
      dto.couponIds,
      dto.adjustPercent,
    );
    return { code: 0, data: result };
  }

  @Get('operations')
  @ApiOperation({ summary: 'List batch operation history' })
  async listOperations(@Req() req: any, @Query() query: ListBatchOperationsQueryDto) {
    const result = await this.couponBatchService.listOperations(
      req.user.issuerId,
      query.page || 1,
      query.limit || 20,
    );
    return { code: 0, data: result };
  }
}
