import { Controller, Get, Post, Body, Query, UseGuards, Request } from '@nestjs/common';
import { OperationLogsService } from './operation-logs.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';
import { RequirePermission } from '../auth/decorators/permission.decorator';
import { extractRequestInfo } from '../common/request-utils';
import { CreateAuditLogDto, QueryOperationLogsDto } from './dto/operation-log.dto';

@UseGuards(JwtAuthGuard)
@Controller('operation-logs')
export class OperationLogsController {
  constructor(private service: OperationLogsService) {}

  @Get()
  @RequirePermission('log:view')
  findAll(@Query() query: QueryOperationLogsDto) {
    return this.service.findAll(query);
  }


  @Post('audit')
  @RequirePermission('log:create')
  async createAuditLog(
    @Body() body: CreateAuditLogDto,
    @Request() req: any,
  ) {
    const { ipAddress, userAgent } = extractRequestInfo(req);
    return this.service.log({
      userId: req.user?.id,
      username: req.user?.username,
      module: body.module,
      action: body.action,
      targetId: body.targetId,
      targetType: body.targetType,
      detail: body.detail,
      ipAddress,
      userAgent,
    });
  }
}
