import { Controller, Get, Post, Put, Delete, Body, Param, UseGuards } from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { PrismaService } from '../../prisma/prisma.service';

@ApiTags('分类')
@Controller('categories')
export class CategoriesController {
  constructor(private prisma: PrismaService) {}

  @Get()
  async findAll() {
    return this.prisma.category.findMany({
      orderBy: { sortOrder: 'asc' },
      include: { _count: { select: { courses: true, prompts: true, tools: true, contents: true } } },
    });
  }

  @Get(':id')
  async findById(@Param('id') id: string) {
    return this.prisma.category.findUnique({ where: { id: +id } });
  }

  @Post()
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async create(@Body() body: { name: string; slug: string; description?: string; sortOrder?: number }) {
    return this.prisma.category.create({ data: body });
  }

  @Put(':id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async update(@Param('id') id: string, @Body() body: any) {
    return this.prisma.category.update({ where: { id: +id }, data: body });
  }

  @Delete(':id')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  async remove(@Param('id') id: string) {
    return this.prisma.category.delete({ where: { id: +id } });
  }
}
