import { Body, Controller, Get, HttpStatus, Post, Res, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse as SwaggerApiResponse, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { JwtAuthGuard } from '../../gateway/auth/jwt_auth.guard';
import { CurrentUser } from '../../gateway/auth/current_user.decorator';
import { JwtPayload } from '../../core/login_core/login_core.service';
import { MallService } from './mall.service';
import { PurchaseMallItemDto } from './dto/purchase_mall_item.dto';

@ApiTags('shop')
@ApiBearerAuth()
@Controller('shop')
@UseGuards(JwtAuthGuard)
export class MallController {
  constructor(private readonly mallService: MallService) {}

  @ApiOperation({
    summary: '获取当前账号商城数据',
    description: '返回当前账号钱包余额、商城分类和每个商品的用户维度状态。',
  })
  @SwaggerApiResponse({
    status: 200,
    description: '商城数据获取成功',
  })
  @Get('catalog')
  async getCatalog(@CurrentUser() user: JwtPayload, @Res() res: Response): Promise<void> {
    const data = await this.mallService.getCatalog(BigInt(user.sub));
    res.status(HttpStatus.OK).json({
      success: true,
      data,
      message: '商城数据获取成功',
    });
  }

  @ApiOperation({
    summary: '购买商城商品',
    description: '当前阶段支持购买角色皮肤，并返回账号已拥有皮肤列表。',
  })
  @ApiBody({ type: PurchaseMallItemDto })
  @SwaggerApiResponse({
    status: 200,
    description: '购买成功',
  })
  @Post('purchases')
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async purchase(
    @CurrentUser() user: JwtPayload,
    @Body() purchaseDto: PurchaseMallItemDto,
    @Res() res: Response,
  ): Promise<void> {
    const data = await this.mallService.purchaseItem(BigInt(user.sub), purchaseDto.item_id);
    res.status(HttpStatus.OK).json({
      success: true,
      data,
      message: '购买成功',
    });
  }

}
