import { date } from 'alga-js'
import refreshTokenHelper from "../../../../utils/refreshTokenHelper"
import getTokenHelper from "../../../../utils/getTokenHelper"
import errorHandlingHelper from "../../../../utils/errorHandlingHelper"
import fetchHelper from "../../../../utils/fetchHelper"
import dhlHelper from "../../../../utils/dhlHelper"
import dhlCustomHelper from "../../../../utils/dhlCustomHelper"
import laravelHelper from "../../../../utils/laravelHelper"
import strapiHelper from "../../../../utils/strapiHelper"
import { commissionTableFkPatch } from "../../../../utils/commissionTable"

// Convert dimension value to cm:
// - No decimal point → value is in mm → divide by 10
// - Has decimal (. or ,) → value is in meters → multiply by 100
const toCm = (val: any): number => {
  if (val === null || val === undefined || val === '') return 0
  const raw = String(val).trim()
  if (raw === '' || raw === '0') return 0
  const hasDecimal = raw.includes('.') || raw.includes(',')
  const numeric = parseFloat(raw.replace(',', '.'))
  if (isNaN(numeric) || numeric <= 0) return 0
  const cm = hasDecimal ? numeric * 100 : numeric / 10
  return parseFloat(cm.toFixed(2))
}

/**
 * Multi-Parcel DHL Label Creation Endpoint
 *
 * This endpoint creates multiple DHL labels for a single shipment (M_InOut)
 * where the items are split across multiple parcels based on weight limits.
 *
 * The first tracking number is saved to M_InOut.TrackingNo (for marketplace confirmation)
 * Additional tracking numbers are saved to M_InOut.trackingnoarray as JSON array
 */

// Helper function to save CN23 customs document to order attachments via Strapi
const saveCustomsDocToOrder = async (event: any, orderId: number, orderUid: string, customsDocBase64: string, shipmentNo: string) => {
  const config = useRuntimeConfig()
  try {
    let strapiAttachmentId: number | null = null

    const existingResponse: any = await strapiHelper(event, `ad-attachments?filters[AD_Table_ID][$eq]=259&filters[Record_ID][$eq]=${orderId}`, 'GET', null)

    if(existingResponse?.data?.[0]?.id) {
      strapiAttachmentId = existingResponse.data[0].id
    } else {
      const newAttachmentResp: any = await strapiHelper(event, `ad-attachments`, 'POST', {
        data: {
          AD_Table_ID: 259,
          Record_ID: orderId,
          Record_UU: orderUid
        }
      })
      if(newAttachmentResp?.data?.id) {
        strapiAttachmentId = newAttachmentResp.data.id
      }
    }

    if(strapiAttachmentId) {
      const timestamp = date.now().replace(/[-:]/g, '_').replace(' ', '-')
      const fileName = `CN23-${shipmentNo}-${timestamp}.pdf`
      const pdfBuffer = Buffer.from(customsDocBase64, 'base64')

      const FormData = (await import('form-data')).default
      const formData = new FormData()
      formData.append('field', 'attachment')
      formData.append('ref', 'api::ad-attachment.ad-attachment')
      formData.append('refId', String(strapiAttachmentId))
      formData.append('files', pdfBuffer, {
        filename: fileName,
        contentType: 'application/pdf'
      })
      formData.append('fileInfo', JSON.stringify({
        caption: `CN23 Customs Document - Multi-Parcel Shipment ${shipmentNo}`,
        alternativeText: `CN23-${shipmentNo}`,
        name: fileName
      }))

      const uploadResponse = await $fetch(`${config.api.strapi}/upload`, {
        method: 'POST',
        headers: {
          Authorization: `Bearer ${config.api.strapitoken}`,
          ...formData.getHeaders()
        },
        body: formData
      })

      return { success: true, strapiAttachmentId, uploadResponse }
    }

    return { success: false, error: 'Could not get or create attachment record' }
  } catch(error: any) {
    console.error('[CN23 Multi-Parcel] Error saving customs document to Strapi:', error)
    return { success: false, error: error.message || 'Unknown error' }
  }
}

const handleInoutFunc = async (event: any, authToken: any = null, option: any) => {
  let data: any = {}
  const token = authToken ?? await getTokenHelper(event)

  // Build tracking number array for additional parcels
  const trackingNoArray = option.all_tracking_numbers.slice(1) // All except the first one

  const trackingPayload: any = {
    TrackingNo: option.tracking_number, // First tracking number (for marketplace confirmation)
    DHL_Shipment_Code: option.shipment_code,
    DHL_Routing_Code: option.routing_code,
    DHL_Label_Base64: option.label_base64,
    IsCommissioned: true,
    shipping_date: date.now('', '', {timeZone: 'UTC'}).replace(' ', 'T')+'Z',
    shipping_service_name: option.shippingService,
    tableName: 'M_Inout',
    // Store additional tracking numbers in the new array field
    trackingnoarray: trackingNoArray.length > 0 ? JSON.stringify(trackingNoArray) : null
  }

  if(option.paket_type) {
    trackingPayload['M_Paket_Type_ID'] = option.paket_type
  }
  // Link the commissioning table (Tisch) so the correct camera's video can be
  // resolved later. No-op when the printer is unknown/unmapped.
  Object.assign(trackingPayload, await commissionTableFkPatch(event, option.shippingPrinter, token))

  const resp: any = await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, trackingPayload)
  if(resp) {
    data['shipment'] = resp
    data['status'] = 200
    data['message'] = ''

    if(resp?.IsCommissionedConfirmed !== true) {
      const resp2: any = await fetchHelper(event, 'models/m_inout/'+option.inout_id+'?$expand=c_order_id,m_inoutline,c_bpartner_location_id,ad_org_id', 'GET', token, null)

      // Helper function to format order number with first word of company name
      const formatOrderNumber = () => {
        const documentNo = resp2?.C_Order_ID?.DocumentNo ?? '0'
        const companyName = resp2?.AD_Org_ID?.companyname ?? ''
        const firstWord = companyName.trim().split(/\s+/)[0] || ''
        return firstWord ? `${documentNo}-${firstWord}` : documentNo
      }

      if(resp2?.C_Order_ID?.C_OrderSource_ID?.id) {
        const resp3: any = await fetchHelper(event, 'models/c_ordersource/'+resp2.C_Order_ID.C_OrderSource_ID.id, 'GET', token, null)

        // Marketplace confirmations use only the FIRST tracking number
        // This is the same behavior as single-parcel mode
        if(resp2?.C_Order_ID?.shopware6_order_id) {
          if(resp3?.marketplace_url) {
            try {
              const resp4: any = await laravelHelper(event, 'sales/orders/mark-shopware-order-delivery', 'POST', {
                marketplace_url: resp3.marketplace_url,
                marketplace_key: resp3.marketplace_key,
                marketplace_secret: resp3.marketplace_secret,
                id: resp2.C_Order_ID.shopware6_order_id,
                trackingCodes: [option.tracking_number], // Only first tracking number
                mail: {
                  email: option.customerEmail,
                  isSentCustomTrackingMail: option.isSentCustomTrackingMail,
                  orderNumber: formatOrderNumber(),
                  name: resp2?.C_Order_ID?.C_BPartner?.identifier ?? 'Mr/Ms',
                  company: 'LogYou GmbH',
                  carrier: option.shippingService ?? 'DHL',
                  lines: resp2?.m_inoutline?.filter((i: any) => i.M_Product_ID && !i.C_Charge_ID).map((i: any) => ({
                    description: i.M_Product_ID.identifier,
                    quantity: i.QtyEntered
                  })) ?? [],
                  address1: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address1 ?? '',
                  address2: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address2 ?? '',
                  city: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.City ?? '',
                  country: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.C_Country_ID?.identifier ?? '',
                  postal: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Postal ?? ''
                }
              })

              if(resp4) {
                await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, {
                  IsCommissionedConfirmed: true,
                  ack_commissioned_laravel: true,
                  tableName: 'M_Inout'
                })
              }
            } catch(err: any) {
              data['shopware'] = errorHandlingHelper(err?.data ?? err, err?.data ?? err)
            }
          }
        }
        if(resp2?.C_Order_ID?.shopify_order_id) {
          if(resp3?.marketplace_url) {
            try {
              const resp4: any = await laravelHelper(event, 'sales/orders/mark-shopify-order-delivery', 'POST', {
                orderSource: resp3,
                id: resp2.C_Order_ID.shopify_order_id,
                trackingCodes: {
                  number: option.tracking_number,
                  url: option.tracking_url,
                  company: 'DHL Express'
                },
                mail: {
                  email: option.customerEmail,
                  isSentCustomTrackingMail: option.isSentCustomTrackingMail,
                  orderNumber: formatOrderNumber(),
                  name: resp2?.C_Order_ID?.C_BPartner?.identifier ?? 'Mr/Ms',
                  company: 'LogYou GmbH',
                  carrier: option.shippingService ?? 'DHL',
                  lines: resp2?.m_inoutline?.filter((i: any) => i.M_Product_ID && !i.C_Charge_ID).map((i: any) => ({
                    description: i.M_Product_ID.identifier,
                    quantity: i.QtyEntered
                  })) ?? [],
                  address1: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address1 ?? '',
                  address2: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address2 ?? '',
                  city: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.City ?? '',
                  country: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.C_Country_ID?.identifier ?? '',
                  postal: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Postal ?? ''
                }
              })

              if(resp4) {
                await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, {
                  IsCommissionedConfirmed: true,
                  ack_commissioned_laravel: true,
                  tableName: 'M_Inout'
                })
              }
            } catch(err: any) {
              data['shopify'] = errorHandlingHelper(err?.data ?? err, err?.data ?? err)
            }
          }
        }
        if(resp2?.C_Order_ID?.amazon_order_id) {
          if(resp3?.marketplace_url) {
            try {
              const resp4: any = await laravelHelper(event, 'sales/orders/mark-amazon-order-delivery', 'POST', {
                orderSource: resp3,
                id: resp2.C_Order_ID.amazon_order_id,
                details: {
                  shippingDate: trackingPayload.shipping_date,
                  carrierCode: 'DHL',
                  shippingMethod: 'Paket',
                  referenceId: resp2?.DocumentNo ?? option.inout_id
                },
                trackingCodes: {
                  number: option.tracking_number,
                  url: option.tracking_url,
                },
                mail: {
                  email: option.customerEmail,
                  isSentCustomTrackingMail: option.isSentCustomTrackingMail,
                  orderNumber: formatOrderNumber(),
                  name: resp2?.C_Order_ID?.C_BPartner?.identifier ?? 'Mr/Ms',
                  company: 'LogYou GmbH',
                  carrier: option.shippingService ?? 'DHL',
                  lines: resp2?.m_inoutline?.filter((i: any) => i.M_Product_ID && !i.C_Charge_ID).map((i: any) => ({
                    description: i.M_Product_ID.identifier,
                    quantity: i.QtyEntered
                  })) ?? [],
                  address1: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address1 ?? '',
                  address2: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address2 ?? '',
                  city: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.City ?? '',
                  country: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.C_Country_ID?.identifier ?? '',
                  postal: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Postal ?? ''
                }
              })

              if(resp4) {
                await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, {
                  IsCommissionedConfirmed: true,
                  ack_commissioned_laravel: true,
                  tableName: 'M_Inout'
                })
              }
            } catch(err: any) {
              data['amazon'] = errorHandlingHelper(err?.data ?? err, err?.data ?? err)
            }
          }
        }
        if(resp2?.C_Order_ID?.plentyone_order_id) {
          if(resp3?.marketplace_url) {
            try {
              const plentyOneWeight = resp2?.m_inoutline?.reduce((acc: any, prev: any) => Number(acc['Weight'] ?? 0) + Number(prev['Weight'] ?? 0), 0)
              // For PlentyOne multi-parcel: send all tracking numbers
              const additionalTrackingNumbers = option.all_tracking_numbers?.slice(1) ?? []
              const resp4: any = await laravelHelper(event, 'sales/orders/mark-plentyone-order-delivery', 'POST', {
                orderSource: resp3,
                id: resp2.C_Order_ID.plentyone_order_id,
                details: {
                  shippingDate: trackingPayload.shipping_date,
                  carrierCode: 'DHL',
                  shippingMethod: 'Paket',
                  referenceId: resp2?.DocumentNo ?? option.inout_id,
                  weight: plentyOneWeight ?? 0
                },
                trackingCodes: {
                  number: option.tracking_number,
                  url: option.tracking_url,
                  trackingNoArray: additionalTrackingNumbers.length > 0 ? additionalTrackingNumbers : null,
                },
                mail: {
                  email: option.customerEmail,
                  isSentCustomTrackingMail: option.isSentCustomTrackingMail,
                  orderNumber: formatOrderNumber(),
                  name: resp2?.C_Order_ID?.C_BPartner?.identifier ?? 'Mr/Ms',
                  company: 'LogYou GmbH',
                  carrier: option.shippingService ?? 'DHL',
                  lines: resp2?.m_inoutline?.filter((i: any) => i.M_Product_ID && !i.C_Charge_ID).map((i: any) => ({
                    description: i.M_Product_ID.identifier,
                    quantity: i.QtyEntered
                  })) ?? [],
                  address1: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address1 ?? '',
                  address2: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address2 ?? '',
                  city: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.City ?? '',
                  country: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.C_Country_ID?.identifier ?? '',
                  postal: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Postal ?? ''
                }
              })
              const plentyConfirmLog = JSON.stringify({
                marketplace: 'plentyone',
                endpoint: 'sales/orders/mark-plentyone-order-delivery',
                at: new Date().toISOString(),
                ok: !!resp4,
                resp: resp4
              }).slice(0, 8000)
              if(resp4) {
                await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, {
                  IsCommissionedConfirmed: true,
                  ack_commissioned_laravel: true,
                  marketplace_confirm_log: plentyConfirmLog,
                  tableName: 'M_Inout'
                })
              } else {
                try {
                  await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, {
                    marketplace_confirm_log: plentyConfirmLog,
                    tableName: 'M_Inout'
                  })
                } catch (_logErr) {}
              }
            } catch(err: any) {
              data['plentyone'] = errorHandlingHelper(err?.data ?? err, err?.data ?? err)
              try {
                await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, {
                  marketplace_confirm_log: JSON.stringify({
                    marketplace: 'plentyone',
                    endpoint: 'sales/orders/mark-plentyone-order-delivery',
                    at: new Date().toISOString(),
                    ok: false,
                    error: err?.data ?? err?.message ?? String(err)
                  }).slice(0, 8000),
                  tableName: 'M_Inout'
                })
              } catch (_logErr) {}
            }
          }
        }
        if(resp2?.C_Order_ID?.ExternalOrderId && resp3?.Marketplace?.identifier === 'jtl-ffn') {
          if(resp3?.marketplace_url) {
            try {
              const resp4: any = await laravelHelper(event, 'sales/orders/mark-jtl-order-delivery', 'POST', {
                orderSource: resp3,
                id: resp2.C_Order_ID.ExternalOrderId,
                details: {
                  shippingDate: trackingPayload.shipping_date,
                  carrierCode: 'DHL',
                  shippingMethod: 'Paket',
                  referenceId: resp2?.DocumentNo ?? option.inout_id,
                  weight: 0
                },
                trackingCodes: {
                  number: option.tracking_number,
                  url: option.tracking_url,
                },
                mail: {
                  email: option.customerEmail,
                  isSentCustomTrackingMail: option.isSentCustomTrackingMail,
                  orderNumber: formatOrderNumber(),
                  name: resp2?.C_Order_ID?.C_BPartner?.identifier ?? 'Mr/Ms',
                  company: 'LogYou GmbH',
                  carrier: option.shippingService ?? 'DHL',
                  lines: resp2?.m_inoutline?.filter((i: any) => i.M_Product_ID && !i.C_Charge_ID).map((i: any) => ({
                    description: i.M_Product_ID.identifier,
                    quantity: i.QtyEntered
                  })) ?? [],
                  address1: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address1 ?? '',
                  address2: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address2 ?? '',
                  city: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.City ?? '',
                  country: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.C_Country_ID?.identifier ?? '',
                  postal: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Postal ?? ''
                }
              })

              if(resp4) {
                await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, {
                  IsCommissionedConfirmed: true,
                  ack_commissioned_laravel: true,
                  tableName: 'M_Inout'
                })
              }
            } catch(err: any) {
              data['jtl'] = errorHandlingHelper(err?.data ?? err, err?.data ?? err)
            }
          }
        }
        if(resp2?.C_Order_ID?.ebay_order_id) {
          try {
            const resp4: any = await laravelHelper(event, 'sales/orders/mark-ebay-order-delivery', 'POST', {
              orderSource: resp3,
              id: resp2.C_Order_ID.ebay_order_id,
              details: {
                shippingDate: trackingPayload.shipping_date,
                carrierCode: 'DHL',
                shippingMethod: 'Paket',
                referenceId: resp2?.DocumentNo ?? option.inout_id
              },
              trackingCodes: {
                number: option.tracking_number,
                url: option.tracking_url,
              },
              mail: {
                email: option.customerEmail,
                isSentCustomTrackingMail: option.isSentCustomTrackingMail,
                orderNumber: formatOrderNumber(),
                name: resp2?.C_Order_ID?.C_BPartner?.identifier ?? 'Mr/Ms',
                company: 'LogYou GmbH',
                carrier: option.shippingService ?? 'DHL',
                lines: resp2?.m_inoutline?.map((i: any) => ({
                  description: i.M_Product_ID.identifier,
                  quantity: i.QtyEntered
                })) ?? [],
                address1: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address1 ?? '',
                address2: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Address2 ?? '',
                city: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.City ?? '',
                country: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.C_Country_ID?.identifier ?? '',
                postal: resp2?.C_Order_ID?.C_BPartner_Location_ID?.C_Location_ID?.Postal ?? ''
              }
            })

            if(resp4) {
              await fetchHelper(event, 'models/m_inout/'+option.inout_id, 'PUT', token, {
                IsCommissionedConfirmed: true,
                ack_commissioned_laravel: true,
                tableName: 'M_Inout'
              })
            }
          } catch(err: any) {
            data['ebay'] = errorHandlingHelper(err?.data ?? err, err?.data ?? err)
          }
        }
      }
    }
  }

  return data
}

const handleFunc = async (event: any) => {
  let data: any = {}
  const config = useRuntimeConfig()
  const body = await readBody(event)

  // Validate multi-parcel data
  if (!body.parcels || !Array.isArray(body.parcels) || body.parcels.length === 0) {
    return {
      status: 400,
      message: 'Multi-parcel mode requires parcels array with at least one parcel'
    }
  }

  let dhlCustomActive = false
  let dhlCustomData = {}
  let customerEmail = 'fulfillcustomer@logyou.de'
  let isSentCustomTrackingMail = false
  const tokenEmail = await getTokenHelper(event)

  const respInout: any = await fetchHelper(event, 'models/m_inout/'+body.inOutId+'?$expand=c_order_id,ad_org_id', 'GET', tokenEmail, null)

  if(respInout?.C_Order_ID?.C_OrderSource_ID?.id) {
    const respOrderSource: any = await fetchHelper(event, 'models/c_ordersource/'+respInout.C_Order_ID.C_OrderSource_ID.id, 'GET', tokenEmail, null)
    isSentCustomTrackingMail = respOrderSource?.isSentCustomTrackingMail ?? false
    if(body.email) {
      customerEmail = (respOrderSource?.isExcludetrackingmail ? 'fulfillcustomer@logyou.de' : body.email)
    }
    if(respOrderSource?.DHL_USE_CUSTOM == true && respOrderSource?.DHLURL) {
      dhlCustomActive = true
      dhlCustomData = {
        dhlurl: respOrderSource.DHLURL,
        dhlkey: respOrderSource.DHLKEY,
        dhlsecret: respOrderSource.DHLSECRET,
        dhluser: respOrderSource.DHLUSER,
        dhlpass: respOrderSource.DHLPASS,
        iscustom: respOrderSource.DHL_USE_CUSTOM,
        dhl_billing_number_de: respOrderSource.dhl_billing_number_de || '63291441520101',
        dhl_billing_number_int: respOrderSource.dhl_billing_number_int || '63291441525301'
      }
    }
  }

  let customerPhone = '+49 987654321'
  if(body.telephone) {
    customerPhone = body.telephone
  }

  // Format order number with first word of company name
  const companyName = respInout?.AD_Org_ID?.companyname ?? ''
  const firstWord = companyName.trim().split(/\s+/)[0]?.replace(/[^a-zA-Z0-9]/g, '') || ''
  const baseOrderNumber = body.orderNumber ?? body.inOutUId.replaceAll('-', '')
  const formattedOrderNumber = firstWord ? `${baseOrderNumber}-${firstWord}` : baseOrderNumber

  let refNo = formattedOrderNumber
  if(refNo.length < 9) {
    let refNoDigit = '0'
    for(let i = 0; i < (9 - Number(refNo.length)); i++) {
      refNoDigit += '0'
      refNo = `${refNoDigit}${refNo}`
    }
  }

  // Fetch sender email from the organization's linked C_BPartner
  let orgSenderEmail: string = ''
  const orgId = respInout?.AD_Org_ID?.id
  if (orgId) {
    try {
      const orgRes: any = await fetchHelper(
        event,
        `models/ad_org/${orgId}?$select=C_BPartner_ID`,
        'GET',
        tokenEmail,
        null
      )
      const orgBpartnerId = orgRes?.C_BPartner_ID?.id
      if (orgBpartnerId) {
        const partnerRes: any = await fetchHelper(
          event,
          `models/c_bpartner/${orgBpartnerId}?$select=freight_service_sender_mail`,
          'GET',
          tokenEmail,
          null
        )
        orgSenderEmail = String(partnerRes?.freight_service_sender_mail ?? '').trim()
      }
    } catch (err: any) {
      console.error(`[DHL Multi] Error fetching org sender email:`, err?.message || err)
    }
  }

  // Consignee info (same for all parcels)
  let newConsigneeValue = {}
  if(body.name2) {
    newConsigneeValue = {...newConsigneeValue, name2: String(body.name2 ?? '').trim()}
  }
  if(body.address2) {
    newConsigneeValue = {...newConsigneeValue, name3: String(body.address2 ?? '').trim()}
  }
  if(body.countryState) {
    newConsigneeValue = {...newConsigneeValue, state: String(body.countryState).trim()}
  }

  // Build shipments array for DHL API - one shipment per parcel slot
  const shipments: any[] = []

  for (let i = 0; i < body.parcels.length; i++) {
    const parcel = body.parcels[i]
    const parcelRefNo = `${refNo}-P${i + 1}` // Add parcel suffix to reference

    // Build customs object for this parcel (with CN23 support)
    let customsObj: any = {
      exportType: body.customsExportType || 'OTHER',
      postalCharges: {
        currency: body.totalOrderValueCurrency ?? 'EUR',
        value: 0
      },
      shipperCustomsRef: 'EORIDE321993855047810/0000',
    }

    // exportDescription is the free-text shown on the CN23 ("Comments"). DHL
    // requires it when exportType is OTHER. Chain: explicit modal description
    // (OTHER) > order Description (c_order.Description) > this parcel's joined
    // item descriptions > 'Merchandise' — all capped at 80 chars.
    if(customsObj.exportType === 'OTHER' && body.customsExportDescription && String(body.customsExportDescription).trim()) {
      customsObj.exportDescription = String(body.customsExportDescription).trim().substring(0, 80)
    } else if(body.orderDescription && String(body.orderDescription).trim()) {
      customsObj.exportDescription = String(body.orderDescription).trim().substring(0, 80)
    } else if(customsObj.exportType === 'OTHER') {
      // Fail-safe: DHL rejects OTHER without exportDescription. Derive per parcel
      // from its own items so each CN23 describes its actual contents.
      const itemDesc = (parcel.items || [])
        .map((it: any) => String(it.description ?? '').trim())
        .filter(Boolean)
        .join(', ')
      customsObj.exportDescription = (itemDesc || 'Merchandise').substring(0, 80)
    }

    if(parcel.items && parcel.items.length > 0) {
      customsObj.items = parcel.items.map((item: any) => {
        // DHL CN23 expects itemWeight as PER-UNIT weight; it multiplies by
        // packagedQuantity to compute the CN23 total. Frontend sends line
        // totals (productWeight × quantity), so divide back to per-unit here.
        const lineQty = Math.max(1, Number(item.quantity || 1))
        const perUnitWeight = parseFloat((Number(item.weight || 0) / lineQty).toFixed(3))

        const itemData: any = {
          itemDescription: String(item.description ?? '').trim().substring(0, 256),
          packagedQuantity: item.quantity,
          itemValue: {
            currency: body.totalOrderValueCurrency ?? 'EUR',
            value: item.value
          },
          itemWeight: {
            uom: 'kg',
            value: perUnitWeight > 0 ? perUnitWeight : 0.01
          },
        }
        // Add CN23 specific fields if provided (taricCode = M_Product.taricCode fallback
        // for labels created without the customs modal normalizing it into hsCode)
        if(item.hsCode || item.taricCode) {
          itemData.hsCode = String(item.hsCode || item.taricCode).substring(0, 11)
        }
        if(item.countryOfOrigin) {
          itemData.countryOfOrigin = String(item.countryOfOrigin).substring(0, 3).toUpperCase()
        }
        return itemData
      })
    }

    if(body.orderNumber) {
      customsObj.invoiceNo = String(body.orderNumber).substring(0, 35)
    }

    const payLoad = {
      product: body.country === 'DE' ? 'V01PAK' : 'V53WPAK',
      billingNumber: body.country === 'DE' ? '63807218920101' : '63807218925301',
      refNo: parcelRefNo,
      costCenter: companyName || undefined,
      shipper: {
        name1: respInout?.AD_Org_ID?.companyname && respInout?.AD_Org_ID !== 1000000 ? respInout?.AD_Org_ID?.companyname : 'LogYou GmbH',
        // Only include name2 when non-empty — DHL Parcel DE v2 rejects an empty name2
        // ("Parameter name2 must be between 1 and 50 characters long").
        ...((respInout?.AD_Org_ID?.companyname && respInout?.AD_Org_ID !== 1000000) ? { name2: 'c/o LogYou GmbH' } : {}),
        addressStreet: 'Mühlenweg',
        addressHouse: '4',
        postalCode: '35510',
        city: 'Butzbach',
        country: 'DEU',
        email: orgSenderEmail || 'info@logyou.de',
        phone: '+4960339160570'
      },
      consignee: {
        name1: String(body.name ?? '').trim(),
        addressStreet: String(body.address ?? '').trim(),
        addressHouse: String(body.houseNumber || 0).trim(),
        postalCode: String(body.postalCode ?? '').trim(),
        city: String(body.city ?? '').trim(),
        country: body.countryIso3 || body.countryName?.toUpperCase()?.replaceAll(' ', '')?.slice(0, 3),
        email: String(customerEmail ?? '').trim(),
        phone: String(customerPhone ?? '').trim(),
        ...newConsigneeValue
      },
      details: {
        dim: {
          uom: 'cm',
          height: toCm(body.height),
          length: toCm(body.length),
          width: toCm(body.width)
        },
        weight: {
          uom: 'kg',
          // Use the parcel's specific weight + 0.25kg packaging. Safety net:
          // make sure declared weight is never below the sum of CN23 line
          // totals (Σ itemWeight × packagedQuantity) for this parcel, or DHL
          // rejects with "details.weight is smaller than the one given in the
          // CN23 form".
          value: parseFloat((Math.max(
            Number(parcel.weight ?? 0),
            (parcel.items || []).reduce(
              (sum: number, it: any) => sum + Number(it.weight || 0),
              0
            )
          ) + 0.25).toFixed(2))
        }
      },
      customs: customsObj
    }

    // Add PDDP (Postal Delivery Duty Paid) for USA destinations with customs value ≤ €680
    const isUSA = body.countryIso3 === 'USA' || body.country === 'US'
    if (isUSA && body.enableCN23) {
      const totalCustomsValue = (parcel.items || []).reduce((sum: number, item: any) => sum + Number(item.value || 0), 0)
      if (totalCustomsValue <= 680) {
        (payLoad as any).services = {
          ...((payLoad as any).services || {}),
          postalDeliveryDutyPaid: "true"
        }
      }
    }

    // Override billing number if using custom DHL
    if(dhlCustomActive) {
      payLoad['billingNumber'] = body.country === 'DE' ? dhlCustomData['dhl_billing_number_de'] : dhlCustomData['dhl_billing_number_int']
    }

    shipments.push(payLoad)
  }

  // Call DHL API with all shipments in one request
  let res: any = {}
  if(dhlCustomActive) {
    res = await dhlCustomHelper(event, dhlCustomData, 'shipping/v2/orders?printFormat=910-300-400', 'POST', {
      profile: 'STANDARD_GRUPPENPROFIL',
      shipments: shipments
    })
  } else {
    res = await dhlHelper(event, 'shipping/v2/orders?printFormat=910-300-400', 'POST', {
      profile: 'STANDARD_GRUPPENPROFIL',
      shipments: shipments
    })
  }

  // Process response - extract all tracking numbers, labels, and customs docs
  if(res?.items && res.items.length > 0) {
    const allTrackingNumbers: string[] = []
    const allLabels: any[] = []
    const allCustomsDocs: any[] = []

    for (const item of res.items) {
      if (item.shipmentNo) {
        allTrackingNumbers.push(item.shipmentNo)
        allLabels.push({
          shipmentNo: item.shipmentNo,
          routingCode: item.routingCode,
          label: item.label
        })
        // Extract CN23 customs document if present
        if (item.customsDoc?.b64) {
          allCustomsDocs.push({
            shipmentNo: item.shipmentNo,
            b64: item.customsDoc.b64,
            fileFormat: item.customsDoc.fileFormat || 'PDF'
          })
        }
      }
    }

    data['dhl_parcels'] = allLabels
    data['tracking_numbers'] = allTrackingNumbers
    data['parcel_count'] = allTrackingNumbers.length

    // Return customs docs to frontend for printing
    if (allCustomsDocs.length > 0) {
      data['customsDocs'] = allCustomsDocs
      data['hasCustomsDoc'] = true
    }

    // Save CN23 customs documents to order attachments via Strapi (if present and enableCN23 is true)
    if (allCustomsDocs.length > 0 && body.enableCN23) {
      try {
        const token = await getTokenHelper(event)
        const shipmentData: any = await fetchHelper(event, `models/m_inout/${body.inOutId}?$expand=c_order_id`, 'GET', token, null)

        if (shipmentData?.C_Order_ID?.id) {
          const orderId = shipmentData.C_Order_ID.id
          const orderUid = shipmentData.C_Order_ID.uid || ''

          for (const customsDoc of allCustomsDocs) {
            const strapiResult = await saveCustomsDocToOrder(event, orderId, orderUid, customsDoc.b64, customsDoc.shipmentNo)
            if (strapiResult.success) {
              console.log(`[CN23 Multi-Parcel] Saved customs doc for ${customsDoc.shipmentNo} to order ${orderId}`)
            } else {
              console.error(`[CN23 Multi-Parcel] Failed to save customs doc for ${customsDoc.shipmentNo}:`, strapiResult.error)
            }
          }
        } else {
          console.error('[CN23 Multi-Parcel] Could not find order ID from shipment')
        }
      } catch (strapiError: any) {
        console.error('[CN23 Multi-Parcel] Error saving customs documents to Strapi:', strapiError)
        data['strapiCustomsDocError'] = strapiError.message || 'Unknown error'
      }
    }

    // Save to M_InOut - first tracking number goes to TrackingNo, rest to trackingnoarray
    if (allTrackingNumbers.length > 0) {
      try {
        const firstLabel = allLabels[0]
        const res2 = await handleInoutFunc(event, null, {
          inout_id: body.inOutId,
          tracking_number: allTrackingNumbers[0], // First tracking number for marketplace
          tracking_url: firstLabel.routingCode,
          shipment_code: allTrackingNumbers.join(','), // All tracking numbers comma-separated
          routing_code: firstLabel.routingCode,
          label_base64: firstLabel.label?.b64 || '',
          paket_type: body.paketType,
          shippingService: body.shippingService,
          shippingPrinter: body.shippingPrinter,
          customerEmail: customerEmail,
          isSentCustomTrackingMail: isSentCustomTrackingMail,
          all_tracking_numbers: allTrackingNumbers // For trackingnoarray field
        })
        data = {...data, ...res2}
      } catch(err: any) {
        try {
          let authToken: any = await refreshTokenHelper(event)
          const firstLabel = allLabels[0]
          const res3 = await handleInoutFunc(event, authToken, {
            inout_id: body.inOutId,
            tracking_number: allTrackingNumbers[0],
            tracking_url: firstLabel.routingCode,
            shipment_code: allTrackingNumbers.join(','),
            routing_code: firstLabel.routingCode,
            label_base64: firstLabel.label?.b64 || '',
            paket_type: body.paketType,
            shippingService: body.shippingService,
            shippingPrinter: body.shippingPrinter,
            customerEmail: customerEmail,
            isSentCustomTrackingMail: isSentCustomTrackingMail,
            all_tracking_numbers: allTrackingNumbers
          })
          data = {...data, ...res3}
        } catch(error: any) {
          data = errorHandlingHelper(err?.data ?? err, error?.data ?? error)
        }
      }
    }
  } else {
    data['status'] = 500
    data['message'] = res?.status?.detail ?? 'DHL Multi-Parcel labels could not be created!'
  }

  return data
}

export default defineEventHandler(async (event) => {
  let data: any = {}

  try {
    data = await handleFunc(event)
  } catch(err: any) {
    data = errorHandlingHelper(err?.data ?? err, err?.data ?? err)
  }

  return data
})
