import { Controller, Get, Post, Body, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { RedemptionService } from '../../../application/services/redemption.service';
import {
  ScanRedemptionDto,
  ManualRedemptionDto,
  BatchRedemptionDto,
  ListRedemptionsQueryDto,
} from '../dto/redemption.dto';

@ApiTags('Redemptions')
@Controller('redemptions')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class RedemptionController {
  constructor(private readonly redemptionService: RedemptionService) {}

  @Post('scan')
  @ApiOperation({ summary: 'Redeem a coupon by QR code scan' })
  async scanRedeem(@Req() req: any, @Body() dto: ScanRedemptionDto) {
    const result = await this.redemptionService.scanRedeem(
      req.user.issuerId,
      dto.qrCode,
      dto.storeId,
    );
    return { code: 0, data: result };
  }

  @Post('manual')
  @ApiOperation({ summary: 'Redeem a coupon by manual code entry' })
  async manualRedeem(@Req() req: any, @Body() dto: ManualRedemptionDto) {
    const result = await this.redemptionService.manualRedeem(
      req.user.issuerId,
      dto.couponCode,
      dto.storeId,
    );
    return { code: 0, data: result };
  }

  @Post('batch')
  @ApiOperation({ summary: 'Batch redeem multiple coupons' })
  async batchRedeem(@Req() req: any, @Body() dto: BatchRedemptionDto) {
    const result = await this.redemptionService.batchRedeem(
      req.user.issuerId,
      dto.couponCodes,
      dto.storeId,
    );
    return { code: 0, data: result };
  }

  @Get()
  @ApiOperation({ summary: 'List redemption history' })
  async listRedemptions(@Req() req: any, @Query() query: ListRedemptionsQueryDto) {
    const result = await this.redemptionService.listRedemptions(
      req.user.issuerId,
      query.page || 1,
      query.limit || 20,
      query.startDate,
      query.endDate,
    );
    return { code: 0, data: result };
  }

  @Get('today-stats')
  @ApiOperation({ summary: 'Get today redemption statistics' })
  async getTodayStats(@Req() req: any) {
    const stats = await this.redemptionService.getTodayStats(req.user.issuerId);
    return { code: 0, data: stats };
  }
}
