import { Body, Controller, Get, Param, Patch, Req, UseGuards } from '@nestjs/common';
import { Roles } from '../common/decorators/roles.decorator';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { SimulatePaymentDto } from './dto/simulate-payment.dto';
import { PaymentsService } from './payments.service';

@Controller('payments')
@UseGuards(JwtAuthGuard, RolesGuard)
export class PaymentsController {
  constructor(private readonly paymentsService: PaymentsService) {}

  @Get('transactions')
  @Roles('ADMIN', 'SUPER_ADMIN')
  transactions() {
    return this.paymentsService.listTransactions();
  }

  @Get('overview')
  @Roles('ADMIN', 'SUPER_ADMIN')
  overview() {
    return this.paymentsService.getOverview();
  }

  @Get('providers')
  @Roles('ADMIN', 'SUPER_ADMIN')
  providers() {
    return this.paymentsService.getProvidersStatus();
  }

  @Get('gateway-settings')
  @Roles('ADMIN', 'SUPER_ADMIN')
  gatewaySettings() {
    return this.paymentsService.getGatewaySettings();
  }

  @Get('settlement-policy')
  @Roles('ADMIN', 'SUPER_ADMIN', 'RESTAURANT')
  settlementPolicy() {
    return this.paymentsService.getSettlementPolicy();
  }

  @Patch('gateway-settings')
  @Roles('ADMIN', 'SUPER_ADMIN')
  updateGatewaySettings(@Body() body: Record<string, unknown>) {
    return this.paymentsService.updateGatewaySettings(body);
  }

  @Patch(':id/simulate-webhook')
  @Roles('ADMIN', 'SUPER_ADMIN')
  simulateWebhook(
    @Param('id') id: string,
    @Body() body: SimulatePaymentDto,
    @Req() req: { user: { sub: string } },
  ) {
    return this.paymentsService.simulateWebhook(id, body.status, req.user.sub);
  }
}
