import { Controller, Get, Query, Param, UseGuards, Logger } from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
  ApiQuery,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AuditService, AuditQueryDto } from './audit.service';
import { Roles } from '../../common/decorators/roles.decorator';
import { RolesGuard } from '../../common/guards/roles.guard';
import { UserRole } from '../../common/enums';

@ApiTags('Audit')
@Controller('audit')
@ApiBearerAuth('BearerAuth')
@UseGuards(AuthGuard('jwt'), RolesGuard)
@Roles(UserRole.ADMIN)
export class AuditController {
  private readonly logger = new Logger(AuditController.name);

  constructor(private readonly auditService: AuditService) {}

  @Get()
  @ApiOperation({
    summary: 'Query audit logs',
    description:
      'Get paginated audit logs with optional filters by entity, action, actor, and date range',
  })
  @ApiQuery({
    name: 'entityType',
    required: false,
    description: 'Filter by entity type (REQUEST, DOCUMENT, DEPARTMENT, etc.)',
  })
  @ApiQuery({ name: 'entityId', required: false, description: 'Filter by entity ID' })
  @ApiQuery({
    name: 'action',
    required: false,
    description: 'Filter by action (CREATE, UPDATE, DELETE, APPROVE, REJECT, etc.)',
  })
  @ApiQuery({
    name: 'actorType',
    required: false,
    description: 'Filter by actor type (APPLICANT, DEPARTMENT, SYSTEM, ADMIN)',
  })
  @ApiQuery({ name: 'actorId', required: false, description: 'Filter by actor ID' })
  @ApiQuery({ name: 'startDate', required: false, description: 'Filter from date (ISO 8601)' })
  @ApiQuery({ name: 'endDate', required: false, description: 'Filter to date (ISO 8601)' })
  @ApiQuery({
    name: 'page',
    required: false,
    type: Number,
    description: 'Page number (default: 1)',
  })
  @ApiQuery({
    name: 'limit',
    required: false,
    type: Number,
    description: 'Items per page (default: 20)',
  })
  @ApiResponse({ status: 200, description: 'Paginated audit logs' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden - Admin only' })
  async findAll(@Query() query: AuditQueryDto) {
    this.logger.debug('Querying audit logs');
    return this.auditService.findAll(query);
  }

  @Get('requests/:requestId')
  @ApiOperation({
    summary: 'Get audit trail for request',
    description: 'Get complete audit trail for a specific license request',
  })
  @ApiParam({ name: 'requestId', description: 'Request ID (UUID)' })
  @ApiResponse({ status: 200, description: 'Audit trail for request' })
  async findByRequest(@Param('requestId') requestId: string) {
    return this.auditService.findByEntity('REQUEST', requestId);
  }

  @Get('entity/:entityType/:entityId')
  @ApiOperation({
    summary: 'Get audit trail for entity',
    description: 'Get complete audit trail for a specific entity (e.g., all changes to a request)',
  })
  @ApiParam({
    name: 'entityType',
    description: 'Entity type (REQUEST, DOCUMENT, DEPARTMENT, etc.)',
  })
  @ApiParam({ name: 'entityId', description: 'Entity ID (UUID)' })
  @ApiResponse({ status: 200, description: 'Audit trail for entity' })
  async findByEntity(@Param('entityType') entityType: string, @Param('entityId') entityId: string) {
    return this.auditService.findByEntity(entityType, entityId);
  }

  @Get('metadata')
  @ApiOperation({
    summary: 'Get audit metadata',
    description: 'Get available audit actions, entity types, and actor types for filtering',
  })
  @ApiResponse({ status: 200, description: 'Audit metadata' })
  async getMetadata() {
    return this.auditService.getEntityActions();
  }
}
