import {
  Controller, Get, Post, Body, Query, Req, UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard, Roles, RolesGuard, UserRole } from '@genex/common';
import { AdminComplianceService } from '../../../application/services/admin-compliance.service';
import { Request } from 'express';
import {
  ListSarQueryDto,
  AdminCreateSarReportDto,
  ListAuditLogsQueryDto,
  GenerateReportDto,
} from '../dto/admin-compliance.dto';

@ApiTags('Admin - Compliance & Audit')
@Controller('admin/compliance')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
export class AdminComplianceController {
  constructor(private readonly adminComplianceService: AdminComplianceService) {}

  // ───────────── SAR ─────────────

  @Get('sar')
  @ApiOperation({ summary: 'List SAR reports (paginated)' })
  async listSarReports(@Query() query: ListSarQueryDto) {
    const data = await this.adminComplianceService.listSarReports(
      query.page,
      query.limit,
      query.status,
    );
    return { code: 0, data };
  }

  @Post('sar')
  @ApiOperation({ summary: 'Create a new SAR report' })
  async createSarReport(@Body() body: AdminCreateSarReportDto) {
    const data = await this.adminComplianceService.createSarReport(body);
    return { code: 0, data, message: 'SAR report created' };
  }

  // ───────────── Audit Logs ─────────────

  @Get('audit-logs')
  @ApiOperation({ summary: 'List admin action audit logs (paginated)' })
  async listAuditLogs(@Query() query: ListAuditLogsQueryDto) {
    const data = await this.adminComplianceService.listAuditLogs(
      query.page,
      query.limit,
      query.action,
      query.adminId,
      query.resource,
      query.startDate,
      query.endDate,
    );
    return { code: 0, data };
  }

  // ───────────── Compliance Reports ─────────────

  @Get('reports')
  @ApiOperation({ summary: 'Get compliance reports overview' })
  async listReports() {
    const data = await this.adminComplianceService.listReports();
    return { code: 0, data };
  }

  @Post('reports/generate')
  @ApiOperation({ summary: 'Generate a compliance report' })
  async generateReport(
    @Body() body: GenerateReportDto,
    @Req() req: Request,
  ) {
    const user = req.user as any;
    const ip = req.ip || req.headers['x-forwarded-for'] as string;
    const data = await this.adminComplianceService.generateReport(
      body.reportType,
      user.sub,
      user.email || user.phone || 'admin',
      ip,
    );
    return { code: 0, data, message: 'Report generated successfully' };
  }

  // ───────────── SEC Filing ─────────────

  @Get('sec-filing')
  @ApiOperation({ summary: 'Get SEC filing status' })
  async getSecFiling() {
    const data = await this.adminComplianceService.getSecFilingStatus();
    return { code: 0, data };
  }

  // ───────────── Licenses ─────────────

  @Get('licenses')
  @ApiOperation({ summary: 'Get license management info' })
  async getLicenses() {
    const data = await this.adminComplianceService.getLicenses();
    return { code: 0, data };
  }

  // ───────────── Tax Compliance ─────────────

  @Get('tax')
  @ApiOperation({ summary: 'Get tax compliance overview' })
  async getTaxCompliance() {
    const data = await this.adminComplianceService.getTaxCompliance();
    return { code: 0, data };
  }
}
