import {
  Controller,
  Get,
  Post,
  Put,
  Param,
  Body,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { OrderService } from './order.service';
import { JwtAuthGuard } from '@/common/guards/jwt-auth.guard';
import { CurrentUser } from '@/common/decorators/current-user.decorator';
import {
  CreateOrderDto,
  QueryOrderDto,
  PayOrderDto,
} from './dto/order.dto';

@ApiTags('订单(用户)')
@Controller('app/orders')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class OrderController {
  constructor(private readonly orderService: OrderService) {}

  @Post()
  @ApiOperation({ summary: '创建订单' })
  async create(
    @CurrentUser('sub') userId: number,
    @Body() dto: CreateOrderDto,
  ) {
    return this.orderService.create(userId, dto);
  }

  @Get()
  @ApiOperation({ summary: '我的订单列表' })
  async findMine(
    @CurrentUser('sub') userId: number,
    @Query() query: QueryOrderDto,
  ) {
    return this.orderService.findByUser(userId, query);
  }

  @Get(':orderNo')
  @ApiOperation({ summary: '订单详情' })
  async findByOrderNo(@Param('orderNo') orderNo: string) {
    return this.orderService.findByOrderNo(orderNo);
  }

  @Put(':orderNo/cancel')
  @ApiOperation({ summary: '取消订单' })
  async cancel(
    @CurrentUser('sub') userId: number,
    @Param('orderNo') orderNo: string,
    @Body('reason') reason: string,
  ) {
    return this.orderService.cancelByOrderNo(userId, orderNo, reason);
  }

  @Post('pay')
  @ApiOperation({ summary: '支付订单' })
  async pay(
    @CurrentUser('sub') userId: number,
    @Body() dto: PayOrderDto,
  ) {
    return this.orderService.payByOrderNo(userId, dto.orderNo, dto.paymentMethod);
  }

  @Put(':orderNo/refund')
  @ApiOperation({ summary: '申请退款' })
  async refund(
    @CurrentUser('sub') userId: number,
    @Param('orderNo') orderNo: string,
    @Body('reason') reason: string,
  ) {
    return this.orderService.refundByOrderNo(userId, orderNo, reason);
  }
}
