import { 
  Controller, 
  Get, 
  Post, 
  Put, 
  Delete, 
  Body, 
  Param, 
  Query, 
  UseGuards,
  HttpCode,
  HttpStatus
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { MenuAdminService } from '../../services/admin/MenuAdminService';
import { CreateMenuDto, UpdateMenuDto, QueryMenuDto, BatchUpdateStatusDto } from '../../dto/admin/MenuDto';
import { JwtAuthGuard } from '../../../auth/guards/JwtAuthGuard';
import { RolesGuard } from '../../../auth/guards/RolesGuard';
import { Roles } from '../../../auth/decorators/RolesDecorator';

@ApiTags('菜单管理')
@Controller('adminapi/menu')
@UseGuards(JwtAuthGuard, RolesGuard)
@ApiBearerAuth()
export class MenuController {
  constructor(private readonly menuAdminService: MenuAdminService) {}

  @Post()
  @ApiOperation({ summary: '创建菜单' })
  @ApiResponse({ status: 201, description: '菜单创建成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @Roles('admin')
  async createMenu(@Body() createMenuDto: CreateMenuDto) {
    return await this.menuAdminService.createMenu(createMenuDto);
  }

  @Get()
  @ApiOperation({ summary: '获取菜单列表' })
  @ApiResponse({ status: 200, description: '获取菜单列表成功' })
  @Roles('admin')
  async getMenuList(@Query() queryMenuDto: QueryMenuDto) {
    return await this.menuAdminService.getMenuList(queryMenuDto);
  }

  @Get('tree')
  @ApiOperation({ summary: '获取菜单树' })
  @ApiResponse({ status: 200, description: '获取成功' })
  async getMenuTree(@Query('appType') appType?: string): Promise<any[]> {
    return await this.menuAdminService.getMenuTree(appType || 'admin');
  }

  @Get(':id')
  @ApiOperation({ summary: '获取菜单详情' })
  @ApiResponse({ status: 200, description: '获取菜单详情成功' })
  @ApiResponse({ status: 404, description: '菜单不存在' })
  @Roles('admin')
  async getMenuDetail(@Param('id') id: string) {
    return await this.menuAdminService.getMenuDetail(Number(id));
  }

  @Put(':id')
  @ApiOperation({ summary: '更新菜单' })
  @ApiResponse({ status: 200, description: '菜单更新成功' })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 404, description: '菜单不存在' })
  @Roles('admin')
  async updateMenu(
    @Param('id') id: string,
    @Body() updateMenuDto: UpdateMenuDto
  ) {
    return await this.menuAdminService.updateMenu(Number(id), updateMenuDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除菜单' })
  @ApiResponse({ status: 200, description: '菜单删除成功' })
  @ApiResponse({ status: 400, description: '菜单有子菜单，无法删除' })
  @ApiResponse({ status: 404, description: '菜单不存在' })
  @HttpCode(HttpStatus.OK)
  @Roles('admin')
  async deleteMenu(@Param('id') id: string) {
    return await this.menuAdminService.deleteMenu(Number(id));
  }

  @Delete('batch')
  @ApiOperation({ summary: '批量删除菜单' })
  @ApiResponse({ status: 200, description: '批量删除菜单成功' })
  @ApiResponse({ status: 400, description: '部分菜单有子菜单，无法删除' })
  @HttpCode(HttpStatus.OK)
  @Roles('admin')
  async batchDeleteMenus(@Body() body: { menuIds: number[] }) {
    return await this.menuAdminService.batchDeleteMenus(body.menuIds);
  }

  @Put(':id/status')
  @ApiOperation({ summary: '更新菜单状态' })
  @ApiResponse({ status: 200, description: '菜单状态更新成功' })
  @ApiResponse({ status: 404, description: '菜单不存在' })
  @Roles('admin')
  async updateMenuStatus(
    @Param('id') id: string,
    @Body() body: { status: number }
  ) {
    return await this.menuAdminService.updateMenuStatus(Number(id), body.status);
  }

  @Put('batch/status')
  @ApiOperation({ summary: '批量更新菜单状态' })
  @ApiResponse({ status: 200, description: '批量更新菜单状态成功' })
  @Roles('admin')
  async batchUpdateMenuStatus(@Body() body: BatchUpdateStatusDto) {
    return await this.menuAdminService.batchUpdateMenuStatus(body.menuIds, body.status);
  }

  @Get('stats/overview')
  @ApiOperation({ summary: '获取菜单统计信息' })
  @ApiResponse({ status: 200, description: '获取菜单统计信息成功' })
  @Roles('admin')
  async getMenuStats() {
    return await this.menuAdminService.getMenuStats();
  }

  @Post('export')
  @ApiOperation({ summary: '导出菜单数据' })
  @ApiResponse({ status: 200, description: '导出菜单数据成功' })
  @Roles('admin')
  async exportMenus() {
    return await this.menuAdminService.exportMenus();
  }
} 