import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  Query,
  ParseIntPipe,
} from '@nestjs/common';
import { LogsService } from './logs.service';
import { QueryLogDto } from './dto/query-log.dto';
import { RequirePermission } from '../auth/decorators/require-permission.decorator';

@Controller('logs')
export class LogsController {
  constructor(private readonly logsService: LogsService) {}

  /**
   * 查询日志列表（分页）
   */
  @Get()
  @RequirePermission('log:read')
  async findAll(@Query() queryLogDto: QueryLogDto) {
    return this.logsService.findAll(queryLogDto);
  }

  /**
   * 获取日志统计信息
   */
  @Get('statistics')
  @RequirePermission('log:read')
  async getStatistics(@Query('days') days?: string) {
    const daysNum = days ? parseInt(days, 10) : 7;
    return this.logsService.getStatistics(daysNum);
  }

  /**
   * 查询单条日志详情
   */
  @Get(':id')
  @RequirePermission('log:read')
  async findOne(@Param('id', ParseIntPipe) id: number) {
    return this.logsService.findOne(id);
  }

  /**
   * 批量删除日志
   */
  @Delete()
  @RequirePermission('log:delete')
  async remove(@Body('ids') ids: number[]) {
    return this.logsService.remove(ids);
  }

  /**
   * 清理过期日志
   */
  @Post('clean')
  @RequirePermission('log:delete')
  async cleanOldLogs(@Body('daysToKeep') daysToKeep?: number) {
    return this.logsService.cleanOldLogs(daysToKeep || 90);
  }
}
