import {
  Controller,
  Get,
  Post,
  Body,
  Query,
  UseGuards,
  Param,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { CouponService } from './coupon.service';
import { JwtAuthGuard } from '@/common';
import { CurrentUser } from '@/common/decorators/current-user.decorator';
import { ReceiveCouponDto, QueryUserCouponDto, QueryCouponDto } from './dto/coupon.dto';

@ApiTags('优惠券(用户)')
@Controller('app/coupons')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class CouponController {
  constructor(private readonly couponService: CouponService) {}

  @Get('available')
  @ApiOperation({ summary: '查询可领取的优惠券' })
  async findAvailable(@Query() dto: QueryCouponDto) {
    // 只返回active状态的优惠券
    return this.couponService.findAll({ ...dto, status: 'active' });
  }

  @Post('receive')
  @ApiOperation({ summary: '领取优惠券' })
  async receive(@Body() dto: ReceiveCouponDto, @CurrentUser() user: any) {
    return this.couponService.receive(user.sub, dto.couponId);
  }

  @Get('my')
  @ApiOperation({ summary: '查询我的优惠券' })
  async findMyCoupons(@Query() dto: QueryUserCouponDto, @CurrentUser() user: any) {
    return this.couponService.findUserCoupons(user.id, dto);
  }

  @Get('usable/:orderId')
  @ApiOperation({ summary: '查询订单可用优惠券' })
  async findUsableCoupons(
    @Param('orderId') orderId: number,
    @Query('orderAmount') orderAmount: number,
    @Query('merchantId') merchantId: number,
    @Query('roomId') roomId: number,
    @CurrentUser() user: any,
  ) {
    return this.couponService.findAvailableCoupons(
      user.id,
      orderAmount,
      merchantId,
      roomId,
    );
  }
}
