import { Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { AuditService } from '../../common/audit.service';
import { IamGuard } from '../../common/iam-adapter';

@ApiTags('Audit')
@Controller('audit')
@UseGuards(IamGuard)
@ApiBearerAuth('IAM-auth')
export class AuditController {
  constructor(private auditService: AuditService) {}

  @Get('logs')
  @ApiOperation({
    summary: 'Get audit logs',
    description: 'Retrieve system audit logs with optional filtering',
  })
  @ApiQuery({ name: 'search', required: false, description: 'Search by user email or entity ID' })
  @ApiQuery({ name: 'action', required: false, description: 'Filter by action (CREATE, UPDATE, DELETE, etc.)' })
  @ApiQuery({ name: 'entityType', required: false, description: 'Filter by entity type (Booking, Station, etc.)' })
  async getLogs(
    @Query('search') search?: string,
    @Query('action') action?: string,
    @Query('entityType') entityType?: string,
  ) {
    const filters = {
      search: search || undefined,
      action: action || undefined,
      entityType: entityType || undefined,
    };

    const items = await this.auditService.getLogs(filters);
    return { items };
  }

  @Get('logs/:id')
  @ApiOperation({ summary: 'Get audit log by ID' })
  async getLog(@Param('id') id: string) {
    return this.auditService.getLog(id);
  }
}
