import { Body, Controller, Get, HttpStatus, Param, Put, Res, UseGuards, UsePipes, ValidationPipe } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiResponse as SwaggerApiResponse, ApiTags } from '@nestjs/swagger';
import { Response } from 'express';
import { JwtPayload } from '../../core/login_core/login_core.service';
import { CurrentUser } from '../../gateway/auth/current_user.decorator';
import { JwtAuthGuard } from '../../gateway/auth/jwt_auth.guard';
import { SaveRoomDecorPlacementDto } from './dto/save_room_decor_placement.dto';
import { RoomDecorService } from './room_decor.service';

@ApiTags('room-decor')
@ApiBearerAuth()
@Controller('rooms/me/decor-placements')
@UseGuards(JwtAuthGuard)
export class RoomDecorController {
  constructor(private readonly roomDecorService: RoomDecorService) {}

  @ApiOperation({
    summary: '获取房间家具背包和摆放状态',
    description: '返回当前账号已拥有的房间摆件，以及每个摆件的摆放位置。',
  })
  @SwaggerApiResponse({
    status: 200,
    description: '房间家具背包获取成功',
  })
  @Get()
  async getInventory(@CurrentUser() user: JwtPayload, @Res() res: Response): Promise<void> {
    const data = await this.roomDecorService.getInventory(BigInt(user.sub));
    res.status(HttpStatus.OK).json({
      success: true,
      data,
      message: '房间家具背包获取成功',
    });
  }

  @ApiOperation({
    summary: '保存房间家具摆放',
    description: '保存当前账号某个家具的摆放状态、位置、缩放和层级。',
  })
  @SwaggerApiResponse({
    status: 200,
    description: '家具摆放保存成功',
  })
  @Put(':decorId')
  @UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
  async savePlacement(
    @CurrentUser() user: JwtPayload,
    @Param('decorId') decorId: string,
    @Body() placementDto: SaveRoomDecorPlacementDto,
    @Res() res: Response,
  ): Promise<void> {
    const data = await this.roomDecorService.savePlacement(BigInt(user.sub), {
      ...placementDto,
      decor_id: decorId,
    });
    res.status(HttpStatus.OK).json({
      success: true,
      data,
      message: '家具摆放保存成功',
    });
  }
}
