import { Body, Controller, Get, HttpStatus, Patch, Res, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiBearerAuth, ApiBody, ApiOperation, ApiResponse as SwaggerApiResponse, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { CurrentUser } from '../../gateway/auth/current_user.decorator';
import { JwtAuthGuard } from '../../gateway/auth/jwt_auth.guard';
import { JwtPayload } from '../../core/login_core/login_core.service';
import { PlayerStateService } from './player_state.service';
import { EconomyService } from './economy.service';
import { UpdatePlayerAppearanceDto } from './dto/update_player_appearance.dto';
import { UpdatePlayerProfileAssetsDto } from './dto/update_player_profile_assets.dto';
import { UpdatePlayerSettingsDto } from './dto/update_player_settings.dto';
import { SocialService } from '../social/social.service';
import { UpdateSocialProfileDto } from '../social/dto/social.dto';

@ApiTags('player')
@ApiBearerAuth()
@Controller('player')
@UseGuards(JwtAuthGuard)
export class PlayerController {
  constructor(
    private readonly playerStateService: PlayerStateService,
    private readonly economyService: EconomyService,
    private readonly socialService: SocialService,
  ) {}

  @ApiOperation({ summary: '获取当前玩家快照' })
  @SwaggerApiResponse({ status: 200, description: '玩家快照获取成功' })
  @Get('snapshot')
  async getSnapshot(@CurrentUser() user: JwtPayload, @Res() res: Response): Promise<void> {
    const data = await this.playerStateService.getSnapshot(BigInt(user.sub));
    res.status(HttpStatus.OK).json({ success: true, data, message: '玩家快照获取成功' });
  }

  @ApiOperation({ summary: '获取当前玩家钱包' })
  @SwaggerApiResponse({ status: 200, description: '钱包获取成功' })
  @Get('wallet')
  async getWallet(@CurrentUser() user: JwtPayload, @Res() res: Response): Promise<void> {
    const data = await this.economyService.getWallet(BigInt(user.sub));
    res.status(HttpStatus.OK).json({ success: true, data, message: '钱包获取成功' });
  }

  @ApiOperation({ summary: '更新当前穿戴皮肤' })
  @ApiBody({ type: UpdatePlayerAppearanceDto })
  @SwaggerApiResponse({ status: 200, description: '外观更新成功' })
  @Patch('appearance')
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async updateAppearance(
    @CurrentUser() user: JwtPayload,
    @Body() dto: UpdatePlayerAppearanceDto,
    @Res() res: Response,
  ): Promise<void> {
    const data = await this.playerStateService.updateAppearance(BigInt(user.sub), dto.skin_id);
    res.status(HttpStatus.OK).json({ success: true, data, message: '外观更新成功' });
  }

  @ApiOperation({ summary: '更新当前玩家设置' })
  @ApiBody({ type: UpdatePlayerSettingsDto })
  @SwaggerApiResponse({ status: 200, description: '设置更新成功' })
  @Patch('settings')
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async updateSettings(
    @CurrentUser() user: JwtPayload,
    @Body() dto: UpdatePlayerSettingsDto,
    @Res() res: Response,
  ): Promise<void> {
    const data = await this.playerStateService.updateSettings(BigInt(user.sub), dto.settings);
    res.status(HttpStatus.OK).json({ success: true, data, message: '设置更新成功' });
  }

  @ApiOperation({ summary: '更新当前玩家头像或自定义皮肤资源' })
  @ApiBody({ type: UpdatePlayerProfileAssetsDto })
  @SwaggerApiResponse({ status: 200, description: '玩家资源更新成功' })
  @Patch('profile-assets')
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async updateProfileAssets(
    @CurrentUser() user: JwtPayload,
    @Body() dto: UpdatePlayerProfileAssetsDto,
    @Res() res: Response,
  ): Promise<void> {
    const data = await this.playerStateService.updateProfileAssets(BigInt(user.sub), dto);
    res.status(HttpStatus.OK).json({ success: true, data, message: '玩家资源更新成功' });
  }

  @ApiOperation({ summary: '更新当前玩家社区名片' })
  @Patch('social-profile')
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async updateSocialProfile(
    @CurrentUser() user: JwtPayload,
    @Body() dto: UpdateSocialProfileDto,
    @Res() res: Response,
  ): Promise<void> {
    const data = await this.socialService.updateSocialProfile(BigInt(user.sub), dto);
    res.status(HttpStatus.OK).json({ success: true, data, message: '社区名片已更新' });
  }
}
