import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
} from '@nestjs/common';
import { ResourceService } from './resource.service';
import { CreateLibraryDto, UpdateLibraryDto, CreateResourceItemDto, UpdateResourceItemDto } from './dto/create-resource.dto';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorators/roles.decorator';

@Controller('admin/resources')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
export class ResourceController {
  constructor(private readonly resourceService: ResourceService) {}

  // ==================== 资源库管理 ====================

  @Get('libraries')
  findAllLibraries(@Query() query: any) {
    return this.resourceService.findAllLibraries(query);
  }

  @Get('libraries/:id')
  findLibrary(@Param('id') id: string) {
    return this.resourceService.findLibrary(+id);
  }

  @Post('libraries')
  createLibrary(@Body() dto: CreateLibraryDto, @Request() req: any) {
    return this.resourceService.createLibrary(dto, req.user.userId);
  }

  @Put('libraries/:id')
  updateLibrary(@Param('id') id: string, @Body() dto: UpdateLibraryDto) {
    return this.resourceService.updateLibrary(+id, dto);
  }

  @Delete('libraries/:id')
  deleteLibrary(@Param('id') id: string) {
    return this.resourceService.deleteLibrary(+id);
  }

  // ==================== 资源项目管理 ====================

  @Get('items')
  findAllItems(@Query() query: any) {
    return this.resourceService.findAllItems(query);
  }

  @Get('items/:id')
  findItem(@Param('id') id: string) {
    return this.resourceService.findItem(+id);
  }

  @Post('items')
  createItem(@Body() dto: CreateResourceItemDto) {
    return this.resourceService.createItem(dto);
  }

  @Put('items/:id')
  updateItem(@Param('id') id: string, @Body() dto: UpdateResourceItemDto) {
    return this.resourceService.updateItem(+id, dto);
  }

  @Delete('items/:id')
  deleteItem(@Param('id') id: string) {
    return this.resourceService.deleteItem(+id);
  }

  @Post('items/batch-delete')
  batchDeleteItems(@Body() body: { ids: number[] }) {
    return this.resourceService.batchDeleteItems(body.ids);
  }

  // ==================== 统计数据 ====================

  @Get('stats')
  getStats() {
    return this.resourceService.getStats();
  }
}
