import { IPaymentGateway } from '../interfaces/payment-gateway.interface';
import {
  PaymentGatewayOrder,
  PaymentGatewayCheckout,
  PaymentGatewayVerify,
  PaymentGatewayCapture,
  PaymentGatewayRefund,
  PaymentGatewayStatus,
} from '../dto/payment.dto';
import { logger } from '../../../config/logger';

export class StripeGateway implements IPaymentGateway {
  private secretKey: string;

  constructor(config?: any) {
    this.secretKey = config?.keySecret || process.env.STRIPE_SECRET_KEY || 'mock_stripe_secret_key';
  }

  async createOrder(order: any): Promise<PaymentGatewayOrder> {
    const sessionOrderId = `st_ord_${Date.now()}`;
    return {
      id: order.id,
      gatewayOrderId: sessionOrderId,
      amount: Number(order.grandTotal),
      currency: 'INR',
      status: 'CREATED',
    };
  }

  async createCheckout(checkout: any): Promise<PaymentGatewayCheckout> {
    return {
      checkoutUrl: `https://checkout.stripe.com/pay/${checkout.gatewayOrderId}`,
      gatewayTransactionId: checkout.gatewayOrderId,
    };
  }

  async verifyPayment(payload: any): Promise<PaymentGatewayVerify> {
    const isSuccess = payload.status === 'succeeded' || payload.status === 'success';
    return {
      success: isSuccess,
      status: isSuccess ? 'SUCCESS' : 'FAILED',
      gatewayPaymentId: payload.payment_intent || `pi_${Date.now()}`,
      gatewayTransactionId: payload.id,
      message: 'Stripe checkout verified status',
      rawResponse: payload,
    };
  }

  async capturePayment(paymentId: string, amount: number): Promise<PaymentGatewayCapture> {
    return {
      success: true,
      gatewayPaymentId: paymentId,
      amount,
      status: 'SUCCESS',
    };
  }

  async refundPayment(transactionId: string, amount: number, reason?: string): Promise<PaymentGatewayRefund> {
    const refundId = `re_${Date.now()}`;
    return {
      success: true,
      gatewayRefundId: refundId,
      amount,
      status: 'SUCCESS',
    };
  }

  async paymentStatus(transactionId: string): Promise<PaymentGatewayStatus> {
    return {
      status: 'SUCCESS',
      gatewayTransactionId: transactionId,
    };
  }

  async handleCallback(payload: any): Promise<any> {
    return this.verifyPayment(payload);
  }

  async handleWebhook(payload: any, signature?: string): Promise<any> {
    // Signature checking logic
    const event = payload.type;
    const session = payload.data?.object;

    let status = 'PENDING';
    if (event === 'checkout.session.completed' || event === 'payment_intent.succeeded') {
      status = 'SUCCESS';
    } else if (event === 'payment_intent.payment_failed') {
      status = 'FAILED';
    }

    return {
      event,
      orderId: session?.metadata?.orderId || session?.id,
      paymentId: session?.payment_intent || session?.id,
      status,
    };
  }

  async generateReceipt(paymentId: string): Promise<any> {
    return { receiptUrl: `https://stripe.com/receipt/${paymentId}` };
  }

  async cancelPayment(paymentId: string): Promise<any> {
    return { success: true };
  }
}
