import { Body, Controller, Get, Param, Post, Query, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiQuery, ApiOkResponse } from '@nestjs/swagger';
import { PaymentsService } from './payments.service';
import { InitiatePaymentDto, RefundDto, AddPaymentMethodDto, PaymentRegionEnum, SupportedPaymentMethodDto } from './payments.dto';
import { JwtGuard } from '../../common/jwt.guard';
import { RolesGuard } from '../../common/roles.guard';
import { Roles } from '../../common/roles.decorator';
import { UserRole } from '@prisma/client';

@ApiTags('Payment')
@Controller('payments')
export class PaymentsController {
  constructor(private service: PaymentsService) {}
  
  @Post('initiate')
  @ApiOperation({ 
    summary: 'Initiate payment with nationality-based payment methods',
    description: `Initiates payment for a booking with support for multiple payment providers:

**Ethiopian Payment Methods:**
- TELEBIRR - Ethiopia's leading mobile money
- CBE_BIRR - Commercial Bank of Ethiopia
- EBIRR - Electronic payment gateway

**Djiboutian Payment Methods:**
- WAAFI - Djibouti's mobile money service

**International Payment Methods:**
- CARD - Visa, Mastercard
- WALLET - Internal wallet balance

**Multi-Currency:**
- All transactions processed in ETB
- Display amounts in ETB, DJF, or USD
- Real-time exchange rate conversion`
  })
  initiatePayment(@Body() dto: InitiatePaymentDto) { return this.service.initiatePayment(dto); }
  
  @Get('intents/:bookingId')
  @ApiOperation({ summary: 'Get payment intent status for a booking' })
  getIntent(@Param('bookingId') bookingId: string) { return this.service.getIntentByBookingId(bookingId); }
  
  @Post('refund')
  @UseGuards(JwtGuard, RolesGuard)
  @Roles(UserRole.ADMIN, UserRole.STAFF, UserRole.AGENT)
  @ApiBearerAuth('JWT-auth')
  @ApiOperation({ summary: 'Refund a confirmed booking (staff/agent only)' })
  refund(@Body() dto: RefundDto) { return this.service.refund(dto); }

  @Post('methods')
  @UseGuards(JwtGuard, RolesGuard)
  @Roles(UserRole.ADMIN, UserRole.STAFF)
  @ApiBearerAuth('JWT-auth')
  @ApiOperation({ summary: 'Add a payment system to the platform catalog (admin only)' })
  addMethod(@Body() dto: AddPaymentMethodDto) { return this.service.addPaymentMethod(dto); }
  
  @Get('methods')
  @ApiOperation({
    summary: 'List payment systems supported by the platform',
    description: 'Returns the global catalog of accepted payment systems. Not user-specific. Optionally filter by region to match a passenger\'s nationality.',
  })
  @ApiQuery({ name: 'region', enum: PaymentRegionEnum, required: false })
  @ApiOkResponse({ type: [SupportedPaymentMethodDto] })
  getMethods(@Query('region') region?: PaymentRegionEnum) { return this.service.getSupportedPaymentMethods(region); }
}
