import { Controller, Post, Get, Body, Param, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiBody } from '@nestjs/swagger';
import { IsNumber, IsString, IsOptional, IsIn } from 'class-validator';
import { AuthGuard } from '@nestjs/passport';
import { OrdersService } from './orders.service';

class CreateOrderDto {
  @IsNumber()
  amount: number;

  @IsString()
  @IsIn(['MONTHLY', 'YEARLY', 'COURSE', 'PACKAGE', 'SKILL'])
  planType: string;

  @IsOptional()
  @IsString()
  skillId?: string;

  @IsOptional()
  @IsString()
  payChannel?: string;

  @IsOptional()
  @IsString()
  @IsIn(['JSAPI', 'NATIVE', 'MWEB'])
  tradeType?: string;

  @IsOptional()
  @IsString()
  openid?: string;
}

@ApiTags('订单')
@Controller('orders')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class OrdersController {
  constructor(private ordersService: OrdersService) {}

  @Post('create')
  @ApiBody({ type: CreateOrderDto })
  async create(@Req() req: any, @Body() body: CreateOrderDto) {
    return this.ordersService.create(req.user.userId, body);
  }

  @Get()
  async findByUser(@Req() req: any, @Query() query: { page?: number; pageSize?: number }) {
    return this.ordersService.findByUser(req.user.userId, query);
  }

  @Get(':orderNo')
  async findByOrderNo(@Param('orderNo') orderNo: string) {
    return this.ordersService.findByOrderNo(orderNo);
  }
}
