import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Query,
  Req,
  UseGuards,
} from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
import { plainToInstance } from 'class-transformer';
import {
  CreateAuthorityDto,
  DeleteAuthorityDto,
  GetAuthMenusResponseDto,
  GetAuthorityListDto,
  GetAuthorityListResponseDto,
  GetAuthorityResponseDto,
  UpdateAuthorityDto,
} from './authority.dto';
import { AuthorityService } from './authority.service';
import { Request } from 'express';
import { AdminJwtAuthGuard } from '../auth/admin/jwt.guard';
import { RolesGuard } from '../auth/admin/roles.guard';
import { Roles } from '../auth/admin/roles.decorator';

@ApiTags('[관리자] 권한')
@UseGuards(AdminJwtAuthGuard, RolesGuard)
@Roles('admin')
@Controller('admin/authority')
export class AdminAuthorityController {
  constructor(private authorityService: AuthorityService) {}

  @ApiOperation({
    summary: `관리자 권한 목록`,
    description: `관리자 권한 목록을 요청합니다`,
  })
  @Get()
  @ApiOkResponse({ type: GetAuthorityListResponseDto })
  async getAuthList(@Query() query: GetAuthorityListDto) {
    const data = await this.authorityService.getAuthList(query);
    return plainToInstance(GetAuthorityListResponseDto, data, {
      excludeExtraneousValues: true,
    });
  }

  @ApiOperation({
    summary: `사용자 권한 매핑된 메뉴 코드 조회`,
  })
  @Get('menus')
  @ApiOkResponse({ type: GetAuthMenusResponseDto })
  async getAuthMenus(@Req() req: Request) {
    const data = await this.authorityService.getAuthMenus(req);
    return plainToInstance(GetAuthMenusResponseDto, data, {
      excludeExtraneousValues: true,
    });
  }

  @ApiOperation({
    summary: `관리자 권한 상세`,
    description: `관리자 권한 상세 및 권한에 따른 메뉴 목록을 요청합니다.`,
  })
  @Get(':athId')
  @ApiOkResponse({ type: GetAuthorityResponseDto })
  async getAuth(@Param('athId') athId: number) {
    const data = await this.authorityService.getAuthDetail(athId);
    return plainToInstance(GetAuthorityResponseDto, data, {
      excludeExtraneousValues: true,
    });
  }

  @ApiOperation({
    summary: `관리자 권한 생성`,
    description: `관리자 권한 신규 등록을 요청합니다.`,
  })
  @Post()
  async createAuth(@Body() body: CreateAuthorityDto) {
    await this.authorityService.createAuth(body);
    return;
  }

  @ApiOperation({
    summary: `관리자 권한 수정`,
    description: `관리자 권한 수정을 요청합니다.`,
  })
  @Post('edit/:athId')
  async updateAuth(
    @Param('athId') athId: number,
    @Body() body: UpdateAuthorityDto,
  ) {
    await this.authorityService.updateAuth(athId, body);
    return;
  }

  @ApiOperation({
    summary: `관리자 권한 삭제`,
  })
  @Post('delete')
  async DeleteAuthorityDto(@Body() body: DeleteAuthorityDto) {
    await this.authorityService.deleteAuth(body);
    return;
  }
}
