import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
  Request,
} from '@nestjs/common';
import { GuestService } from './guest.service';
import { CreateGuestDto, UpdateGuestDto } from './dto/guest.dto';
import { JwtAuthGuard } from '@/common/guards/jwt-auth.guard';

@Controller('app/guests')
@UseGuards(JwtAuthGuard)
export class GuestController {
  constructor(private readonly guestService: GuestService) {}

  @Post()
  async create(@Request() req, @Body() dto: CreateGuestDto) {
    const userId = req.user.sub;
    const guest = await this.guestService.create(userId, dto);
    return { code: 0, message: '添加成功', data: guest };
  }

  @Get()
  async findAll(@Request() req) {
    const userId = req.user.sub;
    const guests = await this.guestService.findAll(userId);
    return { code: 0, data: guests };
  }

  @Get(':id')
  async findOne(@Request() req, @Param('id') id: string) {
    const userId = req.user.sub;
    const guest = await this.guestService.findById(Number(id), userId);
    return { code: 0, data: guest };
  }

  @Put(':id')
  async update(
    @Request() req,
    @Param('id') id: string,
    @Body() dto: UpdateGuestDto,
  ) {
    const userId = req.user.sub;
    const guest = await this.guestService.update(Number(id), userId, dto);
    return { code: 0, message: '更新成功', data: guest };
  }

  @Delete(':id')
  async delete(@Request() req, @Param('id') id: string) {
    const userId = req.user.sub;
    await this.guestService.delete(Number(id), userId);
    return { code: 0, message: '删除成功' };
  }

  @Put(':id/default')
  async setDefault(@Request() req, @Param('id') id: string) {
    const userId = req.user.sub;
    await this.guestService.setDefault(Number(id), userId);
    return { code: 0, message: '设置成功' };
  }
}
