import { ShippingCarrier } from '../../../types/enum.js'

interface MockRate {
  carrier: string
  service_name: string
  service_code: string
  amount: number
  currency: string
  delivery_date?: string
  delivery_days: number
}

/**
 * Generate mock shipping rates for testing purposes
 * Rates vary based on weight, distance, and carrier
 */
export class MockRatesService {
  /**
   * Calculate base rate based on weight and distance
   */
  //   private static calculateBaseRate(params: InternationalRateParams): number {
  //     const { weight, originCountry, destinationCountry } = params
  // console.log(params, 'params')
  //     // Base rate calculation
  //     let baseRate = 10 // Starting rate in BHD

  //     // Weight-based pricing (per kg)
  //     baseRate += weight * 3.5

  //     // Distance/zone pricing
  //     const isGCC = ['BH', 'SA', 'AE', 'KW', 'QA', 'OM', 'AF'].includes(destinationCountry)
  //     const isMiddleEast = ['JO', 'LB', 'EG', 'IQ'].includes(destinationCountry)

  //     if (isGCC) {
  //       baseRate *= 1.0 // GCC countries - base rate
  //     } else if (isMiddleEast) {
  //       baseRate *= 1.3 // Middle East - 30% more
  //     } else {
  //       baseRate *= 3.0 // International - double
  //     }

  //     return baseRate
  //   }
  private static calculateBaseRate(params: any): number {
    const { weight, length, width, height, numberOfPieces, destinationCountry } = params

    let baseRate = 10 // Base price in BHD

    // 1️⃣ Volumetric weight per piece (cm → kg)
    const volumetricWeightPerPiece = (length * width * height) / 5000

    // 2️⃣ Total weights
    const totalVolumetricWeight = volumetricWeightPerPiece * numberOfPieces
    const totalActualWeight = weight * numberOfPieces

    // 3️⃣ Chargeable weight
    const chargeableWeight = Math.max(totalActualWeight, totalVolumetricWeight)

    // 4️⃣ Weight-based pricing
    baseRate += chargeableWeight * 3.5

    // 5️⃣ Zone multiplier
    const isGCC = ['BH', 'SA', 'AE', 'KW', 'QA', 'OM'].includes(destinationCountry)
    const isMiddleEast = ['JO', 'LB', 'EG', 'IQ'].includes(destinationCountry)

    if (isGCC) {
      baseRate *= 1.0
    } else if (isMiddleEast) {
      baseRate *= 1.3
    } else {
      baseRate *= 3.0
    }

    return Number(baseRate.toFixed(2))
  }

  /**
   * Calculate delivery days based on destination
   */
  private static calculateDeliveryDays(
    destinationCountry: string,
    serviceType: 'express' | 'standard' | 'economy'
  ): number {
    const isGCC = ['BH', 'SA', 'AE', 'KW', 'QA', 'OM'].includes(destinationCountry)
    const isMiddleEast = ['JO', 'LB', 'EG', 'IQ'].includes(destinationCountry)

    let baseDays = 1
    if (isGCC) {
      baseDays = 2
    } else if (isMiddleEast) {
      baseDays = 4
    } else {
      baseDays = 6
    }

    // Adjust based on service type
    if (serviceType === 'express') {
      return baseDays
    } else if (serviceType === 'standard') {
      return baseDays + 2
    } else {
      return baseDays + 4
    }
  }

  /**
   * Get delivery date string
   */
  private static getDeliveryDate(days: number): string {
    const date = new Date()
    date.setDate(date.getDate() + days)
    return date.toISOString().split('T')[0]
  }

  /**
   * Generate DHL rates
   */
  static getDHLRates(params: any): MockRate[] {
    const baseRate = this.calculateBaseRate(params)

    return [
      {
        carrier: ShippingCarrier.DHL,
        service_name: 'DHL Express Worldwide',
        service_code: 'DHL_EXPRESS',
        amount: Number.parseFloat((baseRate * 1.7).toFixed(3)), // DHL is premium
        currency: 'BHD',
        delivery_days: this.calculateDeliveryDays(params.destinationCountry, 'express'),
        delivery_date: this.getDeliveryDate(
          this.calculateDeliveryDays(params.destinationCountry, 'express')
        ),
      },
      //   {
      //     carrier: ShippingCarrier.DHL,
      //     service_name: 'DHL Economy Select',
      //     service_code: 'DHL_ECONOMY',
      //     amount: parseFloat((baseRate * 0.85).toFixed(3)),
      //     currency: 'BHD',
      //     delivery_days: this.calculateDeliveryDays(params.destinationCountry, 'economy'),
      //     delivery_date: this.getDeliveryDate(
      //       this.calculateDeliveryDays(params.destinationCountry, 'economy')
      //     ),
      //   },
    ]
  }

  /**
   * Generate FedEx rates
   */
  static getFedExRates(params: any): MockRate[] {
    console.log(params)
    return [
      {
        carrier: ShippingCarrier.FedEx,
        service_name: 'FedEx International Priority',
        service_code: 'FEDEX_PRIORITY',
        amount: 110,
        currency: 'BHD',
        delivery_days: 0,
        delivery_date: '',
      },
      //   {
      //     carrier: ShippingCarrier.FedEx,
      //     service_name: 'FedEx International Economy',
      //     service_code: 'FEDEX_ECONOMY',
      //     amount: parseFloat((baseRate * 0.9).toFixed(3)),
      //     currency: 'BHD',
      //     delivery_days: this.calculateDeliveryDays(params.destinationCountry, 'standard'),
      //     delivery_date: this.getDeliveryDate(
      //       this.calculateDeliveryDays(params.destinationCountry, 'standard')
      //     ),
      //   },
    ]
  }

  /**
   * Generate Aramex rates
   */
  static getAramexRates(params: any): MockRate[] {
    const baseRate = this.calculateBaseRate(params)

    return [
      {
        carrier: ShippingCarrier.Aramex,
        service_name: 'Aramex Express',
        service_code: 'ARAMEX_EXPRESS',
        amount: Number.parseFloat((baseRate * 0.95).toFixed(3)), // Aramex is competitive
        currency: 'BHD',
        delivery_days: this.calculateDeliveryDays(params.destinationCountry, 'express'),
        delivery_date: this.getDeliveryDate(
          this.calculateDeliveryDays(params.destinationCountry, 'express')
        ),
      },
      // {
      //   carrier: ShippingCarrier.Aramex,
      //   service_name: 'Aramex Standard',
      //   service_code: 'ARAMEX_STANDARD',
      //   amount: Number.parseFloat((baseRate * 0.75).toFixed(3)),
      //   currency: 'BHD',
      //   delivery_days: this.calculateDeliveryDays(params.destinationCountry, 'standard'),
      //   delivery_date: this.getDeliveryDate(
      //     this.calculateDeliveryDays(params.destinationCountry, 'standard')
      //   ),
      // },
    ]
  }

  /**
   * Generate SMSA rates
   */
  static getSMSARates(params: any): MockRate[] {
    const baseRate = this.calculateBaseRate(params)

    return [
      {
        carrier: ShippingCarrier.SMSA,
        service_name: 'SMSA Express',
        service_code: 'SMSA_EXPRESS',
        amount: Number.parseFloat((baseRate * 0.88).toFixed(3)), // SMSA is often cheapest
        currency: 'BHD',
        delivery_days: this.calculateDeliveryDays(params.destinationCountry, 'express'),
        delivery_date: this.getDeliveryDate(
          this.calculateDeliveryDays(params.destinationCountry, 'express')
        ),
      },
      //   {
      //     carrier: ShippingCarrier.SMSA,
      //     service_name: 'SMSA Standard',
      //     service_code: 'SMSA_STANDARD',
      //     amount: parseFloat((baseRate * 0.7).toFixed(3)),
      //     currency: 'BHD',
      //     delivery_days: this.calculateDeliveryDays(params.destinationCountry, 'standard'),
      //     delivery_date: this.getDeliveryDate(
      //       this.calculateDeliveryDays(params.destinationCountry, 'standard')
      //     ),
      //   },
    ]
  }

  /**
   * Get all mock rates
   */
  static getAllRates(params: any): MockRate[] {
    return [
      ...this.getDHLRates(params),
      ...this.getFedExRates(params),
      ...this.getAramexRates(params),
      ...this.getSMSARates(params),
    ]
  }

  /**
   * Get rate for specific carrier
   */
  static getCarrierRate(carrier: ShippingCarrier, params: any): MockRate {
    let rates: MockRate[] = []

    switch (carrier) {
      case ShippingCarrier.DHL:
        rates = this.getDHLRates(params)
        break
      case ShippingCarrier.FedEx:
        rates = this.getFedExRates(params)
        break
      case ShippingCarrier.Aramex:
        rates = this.getAramexRates(params)
        break
      case ShippingCarrier.SMSA:
        rates = this.getSMSARates(params)
        break
    }

    // Return the first (express) service by default
    return rates[0]
  }
}
