import { resolveFileUrl } from '@/utils/image'
import { formatPrice } from '@/utils/format'
import { parseCartSpecText } from '@/utils/cartSpec'

const GOODS_PLACEHOLDER = '/static/logo.png'
const SHOP_PLACEHOLDER = '/static/logo.png'

function formatAddress(addr) {
  if (!addr) return ''
  const region = (addr.regionName || '').replace(/\//g, '')
  return `${region}${addr.detailAddress || ''}`.trim()
}

export function mapGoodsItem(item) {
  if (!item) return null
  const specText = item.specText || '默认'
  return {
    cartItemId: item.cartItemId,
    goodsId: item.goodsId,
    goodsName: item.goodsName || '',
    displayPic: resolveFileUrl(item.mainPic) || GOODS_PLACEHOLDER,
    specText,
    specList: parseCartSpecText(specText),
    serviceDesc: item.serviceDesc || '',
    salePrice: item.salePrice,
    priceText: formatPrice(item.salePrice),
    quantity: Number(item.quantity) || 1,
    maxStock: Number(item.maxStock) || 1,
    subtotal: item.subtotal,
    subtotalText: formatPrice(item.subtotal),
    freightType: item.freightType,
    freightDesc: item.freightDesc || '',
    buyerRemark: item.buyerRemark || ''
  }
}

function mapShopAndAddress(data) {
  const shop = data.shop || {}
  const addr = data.address || null
  return {
    shop: {
      shopId: shop.shopId,
      shopName: shop.shopName || '',
      shopAvatar: resolveFileUrl(shop.shopAvatar) || SHOP_PLACEHOLDER,
      shopStatus: shop.shopStatus
    },
    address: addr
      ? {
          addressId: addr.addressId,
          consigneeName: addr.consigneeName || '',
          mobile: addr.mobile || '',
          regionName: addr.regionName || '',
          detailAddress: addr.detailAddress || '',
          fullAddress: formatAddress(addr),
          isDefault: addr.isDefault === '1'
        }
      : null,
    goodsAmount: data.goodsAmount,
    freightAmount: data.freightAmount,
    freightDesc: data.freightDesc || '',
    payAmount: data.payAmount,
    goodsAmountText: formatPrice(data.goodsAmount),
    freightAmountText: formatPrice(data.freightAmount),
    payAmountText: formatPrice(data.payAmount),
    payType: data.payType || 'WECHAT'
  }
}

/** preview 接口 data → 单商品页面模型 */
export function mapCheckoutPreview(data) {
  if (!data) return null
  const base = mapShopAndAddress(data)
  return {
    ...base,
    goods: mapGoodsItem((data.items || [])[0])
  }
}

/** preview 接口 data → 购物车多商品页面模型 */
export function mapCheckoutPreviewCart(data, remarkMap = {}) {
  if (!data) return null
  const base = mapShopAndAddress(data)
  const goodsList = (data.items || [])
    .map((row) => {
      const item = mapGoodsItem(row)
      if (!item) return null
      if (item.cartItemId && remarkMap[item.cartItemId] != null) {
        item.buyerRemark = remarkMap[item.cartItemId]
      }
      return item
    })
    .filter(Boolean)
  return {
    ...base,
    goodsList
  }
}

/** 会员地址列表行 → 选择弹层 */
export function mapAddressOption(row) {
  if (!row) return null
  const region = (row.regionName || '').replace(/\//g, '')
  const full = row.fullAddress || `${region}${row.detailAddress || ''}`
  return {
    addressId: row.addressId,
    consigneeName: row.consigneeName || '',
    mobile: row.mobile || '',
    fullAddress: full,
    isDefault: row.isDefault === '1'
  }
}
