import { Body, Controller, Headers, HttpCode, HttpStatus, Logger, Post } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import {
  TelebirrWebhookPayload,
  TelebirrWebhookService,
} from './telebirr-webhook.service';
import {
  CbeBirrWebhookPayload,
  CbeBirrWebhookService,
} from './cbe-birr-webhook.service';
import {
  EBirrWebhookPayload,
  EBirrWebhookService,
} from './ebirr-webhook.service';
import {
  CardWebhookPayload,
  CardWebhookService,
} from './card-webhook.service';
import { WaafiWebhookService } from './waafi-webhook.service';

@ApiTags('Payment Webhooks')
@Controller('payments/webhooks')
export class WebhooksController {
  private readonly logger = new Logger(WebhooksController.name);

  constructor(
    private readonly telebirr: TelebirrWebhookService,
    private readonly cbeBirr: CbeBirrWebhookService,
    private readonly eBirr: EBirrWebhookService,
    private readonly card: CardWebhookService,
    private readonly waafi: WaafiWebhookService,
  ) {}

  @Post('telebirr')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ 
    summary: 'Telebirr payment notification callback (Ethiopia)',
    description: 'Webhook endpoint for Telebirr payment status updates. Used by Ethiopian passengers.'
  })
  async receiveTelebirr(@Body() payload: TelebirrWebhookPayload) {
    try {
      await this.telebirr.handle(payload);
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      this.logger.error(`Telebirr webhook handler threw: ${message}`);
    }
    return { code: '0', message: 'OK' };
  }

  @Post('cbe-birr')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ 
    summary: 'CBE Birr payment notification callback (Ethiopia)',
    description: 'Webhook endpoint for Commercial Bank of Ethiopia payment status updates.'
  })
  async receiveCbeBirr(@Body() payload: CbeBirrWebhookPayload) {
    try {
      await this.cbeBirr.handle(payload);
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      this.logger.error(`CBE Birr webhook handler threw: ${message}`);
    }
    return { success: true };
  }

  @Post('ebirr')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ 
    summary: 'eBirr payment notification callback (Ethiopia)',
    description: 'Webhook endpoint for eBirr electronic payment gateway status updates.'
  })
  async receiveEBirr(@Body() payload: EBirrWebhookPayload) {
    try {
      await this.eBirr.handle(payload);
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      this.logger.error(`eBirr webhook handler threw: ${message}`);
    }
    return { code: '0000', message: 'success' };
  }

  @Post('card')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ 
    summary: 'Card payment notification callback (International)',
    description: 'Webhook endpoint for international card payments (Visa, Mastercard) via Stripe.'
  })
  async receiveCard(
    @Body() payload: CardWebhookPayload,
    @Headers('stripe-signature') signature: string,
  ) {
    try {
      await this.card.handle(payload, signature);
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      this.logger.error(`Card webhook handler threw: ${message}`);
    }
    return { received: true };
  }

  @Post('waafi')
  @HttpCode(HttpStatus.OK)
  @ApiOperation({ 
    summary: 'Waafi payment notification callback (Djibouti)',
    description: 'Webhook endpoint for Waafi mobile money payment status updates. Used by Djiboutian passengers.'
  })
  async receiveWaafi(@Body() payload: any) {
    try {
      await this.waafi.handleWebhook(payload);
    } catch (err) {
      const message = err instanceof Error ? err.message : String(err);
      this.logger.error(`Waafi webhook handler threw: ${message}`);
    }
    return { responseCode: '2001', responseMsg: 'Success' };
  }
}
