import { Controller, Get, Query, UseGuards } from '@nestjs/common';
import { Roles, RolesGuard } from '@it0/common';
import { AuditLogRepository } from '../../../infrastructure/repositories/audit-log.repository';

@Controller('api/v1/audit')
export class AuditController {
  constructor(private readonly auditLogRepository: AuditLogRepository) {}

  @Get('logs')
  @UseGuards(RolesGuard)
  @Roles('admin', 'platform_admin', 'platform_super_admin')
  async queryLogs(
    @Query('actionType') actionType?: string,
    @Query('actorType') actorType?: string,
    @Query('actorId') actorId?: string,
    @Query('resourceType') resourceType?: string,
    @Query('from') from?: string,
    @Query('to') to?: string,
    @Query('dateFrom') dateFrom?: string,
    @Query('dateTo') dateTo?: string,
    @Query('page') page?: number,
    @Query('limit') limit?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.auditLogRepository.queryLogs({
      actionType,
      actorType,
      actorId,
      resourceType,
      from: from ?? dateFrom,
      to: to ?? dateTo,
      page: page ? Number(page) : undefined,
      limit: limit ? Number(limit) : (pageSize ? Number(pageSize) : undefined),
    });
  }

  @Get('logs/export')
  @UseGuards(RolesGuard)
  @Roles('admin', 'platform_admin', 'platform_super_admin')
  async exportLogs(@Query('format') format: 'json' | 'csv' = 'json') {
    return this.auditLogRepository.exportLogs(format);
  }
}
