import { Controller, Get, Post, Put, Body, Param, Query, UseGuards, ParseUUIDPipe } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { AmlService } from '../../../application/services/aml.service';
import { OfacService } from '../../../application/services/ofac.service';
import { TravelRuleService } from '../../../application/services/travel-rule.service';
import { SarService } from '../../../application/services/sar.service';
import {
  ListAmlAlertsQueryDto,
  UpdateAlertStatusDto,
  ScreenOfacDto,
  CreateSarReportDto,
} from '../dto/compliance.dto';
import { PaginationQueryDto } from '../dto/pagination.dto';

@ApiTags('Compliance')
@Controller('compliance')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class ComplianceController {
  constructor(
    private readonly amlService: AmlService,
    private readonly ofacService: OfacService,
    private readonly travelRuleService: TravelRuleService,
    private readonly sarService: SarService,
  ) {}

  @Get('aml/alerts')
  @ApiOperation({ summary: 'List AML alerts' })
  async listAlerts(@Query() query: ListAmlAlertsQueryDto) {
    return { code: 0, data: await this.amlService.listAlerts(query.page, query.limit, query.status) };
  }

  @Put('aml/alerts/:id/status')
  @ApiOperation({ summary: 'Update AML alert status' })
  async updateAlert(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() body: UpdateAlertStatusDto,
  ) {
    await this.amlService.updateAlertStatus(id, body.status as any, body.resolution);
    return { code: 0, data: null };
  }

  @Post('ofac/screen')
  @ApiOperation({ summary: 'Screen name against OFAC' })
  async screenOfac(@Body() body: ScreenOfacDto) {
    return { code: 0, data: await this.ofacService.screenName(body.userId, body.name) };
  }

  @Get('sar/reports')
  @ApiOperation({ summary: 'List SAR reports' })
  async listSarReports(@Query() query: PaginationQueryDto) {
    return { code: 0, data: await this.sarService.listReports(query.page, query.limit) };
  }

  @Post('sar/reports')
  @ApiOperation({ summary: 'Create SAR report' })
  async createSarReport(@Body() body: CreateSarReportDto) {
    return { code: 0, data: await this.sarService.createReport(body) };
  }

  @Put('sar/reports/:id/file')
  @ApiOperation({ summary: 'File SAR report with FinCEN' })
  async fileSar(@Param('id', ParseUUIDPipe) id: string) {
    await this.sarService.fileReport(id);
    return { code: 0, data: null, message: 'Report filed' };
  }
}
