import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { plainToInstance } from 'class-transformer';
import { AdminJwtAuthGuard } from '../auth/admin/jwt.guard';
import {
  CreateAdminDto,
  DeleteAdminDto,
  GetAdminDto,
  GetAdminListDto,
  GetAdminListResponseDto,
  UpdateAdminDto,
} from './admin.dto';
import { AdminService } from './admin.service';
import { RolesGuard } from '../auth/admin/roles.guard';
import { Roles } from '../auth/admin/roles.decorator';

@ApiTags('[관리자] 관리자')
@UseGuards(AdminJwtAuthGuard, RolesGuard)
@Controller('admin/admin')
@Roles('admin')
export class AdminController {
  constructor(private adminService: AdminService) {}

  @ApiOperation({
    summary: `관리자 목록/검색`,
  })
  @Get()
  @ApiOkResponse({ type: GetAdminListResponseDto })
  async getAdminList(@Query() query: GetAdminListDto) {
    const data = await this.adminService.getAdminList(query);
    return plainToInstance(GetAdminListResponseDto, data);
  }

  @Get(':admId')
  @ApiOperation({
    summary: '관리자 상세조회',
  })
  @ApiOkResponse({ type: GetAdminDto })
  async getAdmin(@Param('admId') admId: number) {
    const data = await this.adminService.getDetail(admId);
    return plainToInstance(GetAdminDto, data);
  }

  @Post()
  @ApiOperation({
    summary: '관리자 신규등록',
  })
  async createAdmin(@Body() body: CreateAdminDto) {
    await this.adminService.createAdmin(body);
    return;
  }

  @Post('edit/:admId')
  @ApiOperation({
    summary: '관리자 수정',
  })
  async updateAdmin(
    @Param('admId') admId: number,
    @Body() body: UpdateAdminDto,
  ) {
    await this.adminService.updateAdmin(admId, body);
    return;
  }

  @Post('delete')
  @ApiOperation({
    summary: '관리자 삭제',
  })
  async deleteAdmin(@Body() body: DeleteAdminDto) {
    await this.adminService.deleteAdmin(body);
    return;
  }
}
