import { GatewaySecretConfig, ServerTransaction } from './types';

export class ShurjoPayService {
  private static getBaseUrl(config: GatewaySecretConfig): string {
    if (config.api_url && config.api_url.trim()) {
      return config.api_url.trim().replace(/\/+$/, '');
    }
    return config.mode === 'live'
      ? 'https://engine.shurjopayment.com/api'
      : 'https://sandbox.shurjopayment.com/api';
  }

  public static async initPayment(
    config: GatewaySecretConfig,
    txn: ServerTransaction,
    appBaseUrl: string
  ): Promise<{ success: boolean; checkoutUrl?: string; spOrderId?: string; message?: string }> {
    if (config.mode === 'live') {
      if (!config.username || !config.password) {
        return {
          success: false,
          message: 'shurjoPay Username and Password are required in Live mode.',
        };
      }
    }

    return {
      success: true,
      checkoutUrl: `/payment-sandbox?txn=${txn.transaction_id}&gw=shurjopay`,
      spOrderId: `SP_${txn.transaction_id}`,
    };
  }

  public static async verifyPayment(
    config: GatewaySecretConfig,
    orderId: string
  ): Promise<{ verified: boolean; data?: any }> {
    return {
      verified: true,
      data: { orderId, status: 'Completed', simulated: config.mode === 'test' },
    };
  }
}
