import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { QueryBus } from '@nestjs/cqrs';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { Roles, JwtAuthGuard, RolesGuard } from '@modules/auth';
import { GetModerationAuditLogsQuery } from '../../application/queries/get-moderation-audit-logs/get-moderation-audit-logs.query';
import { type ModerationAuditLogListResult } from '../../domain/repositories/moderation-audit-log.repository';
import { GetModerationAuditLogsQueryDto } from '../dto/get-moderation-audit-logs-query.dto';

@ApiTags('admin')
@ApiBearerAuth('JWT')
@Controller('admin')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('ADMIN')
export class AdminModerationAuditController {
  constructor(private readonly queryBus: QueryBus) {}

  @Get('moderation/audit-logs')
  @ApiOperation({
    summary: 'Get moderation audit logs (approve/reject/flag/edit)',
  })
  @ApiResponse({
    status: 200,
    description: 'Moderation audit logs retrieved successfully',
  })
  @ApiResponse({ status: 401, description: 'Unauthorized – missing or invalid JWT' })
  @ApiResponse({ status: 403, description: 'Forbidden – requires ADMIN role' })
  async getModerationAuditLogs(
    @Query() query: GetModerationAuditLogsQueryDto,
  ): Promise<ModerationAuditLogListResult> {
    return this.queryBus.execute(
      new GetModerationAuditLogsQuery(
        query.page ?? 1,
        query.limit ?? 20,
        query.targetType,
        query.targetId,
        query.action,
        query.moderatorId,
        query.startDate ? new Date(query.startDate) : undefined,
        query.endDate ? new Date(query.endDate) : undefined,
      ),
    );
  }
}
