import type { StoreItem } from '../services/storeItemService'
import type { ShoppingCard, ShoppingCardItem } from '../services/shoppingCardService'
import type { BusinessCustomer } from '../services/customerService'
import { formatCellForDisplay } from '../lib/cellNumber'
import { hasStoreItemDiscount } from '../utils/irtPrice'

export interface DraftCartItem {
  storeItemVariantId: string
  productId: string
  productTitle: string
  productNameFa: string
  productImage: string | null
  label: string
  unitPrice: number
  compareAtPrice: number | null
  quantity: number
  stockQuantity: number | null
}

export function effectiveUnitPrice(item: StoreItem): number | null {
  if (item.price === null) return null
  if (hasStoreItemDiscount(item.price, item.discountedPrice)) {
    return item.discountedPrice
  }
  return item.price
}

export function storeItemToDraftCartItem(
  variant: StoreItem,
  quantity = 1,
): DraftCartItem | null {
  const unitPrice = effectiveUnitPrice(variant)
  if (unitPrice === null) return null

  return {
    storeItemVariantId: variant.id,
    productId: variant.productId,
    productTitle: variant.productTitle,
    productNameFa: variant.productNameFa,
    productImage: variant.productImage,
    label: variant.label,
    unitPrice,
    compareAtPrice: variant.price,
    quantity,
    stockQuantity: variant.stockQuantity,
  }
}

export function draftCartItemCount(items: DraftCartItem[]): number {
  return items.reduce((sum, item) => sum + item.quantity, 0)
}

export function draftCartSubtotal(items: DraftCartItem[]): number {
  return items.reduce((sum, item) => sum + item.unitPrice * item.quantity, 0)
}

export function shoppingCardCustomerToBusinessCustomer(
  customer: ShoppingCard['customer'],
): BusinessCustomer {
  const name = [customer.firstName, customer.lastName].filter(Boolean).join(' ').trim()
  const display = formatCellForDisplay(customer.cellNumber)
  return {
    id: customer.id,
    cellNumber: customer.cellNumber,
    firstName: customer.firstName,
    lastName: customer.lastName,
    email: customer.email,
    label: name ? `${name} · ${display}` : display,
  }
}

export function shoppingCardItemToDraft(item: ShoppingCardItem): DraftCartItem | null {
  if (!item.storeItemVariantId) return null

  const label = item.selections.length
    ? item.selections.map((selection) => selection.value).join(' · ')
    : item.productTitle

  return {
    storeItemVariantId: item.storeItemVariantId,
    productId: item.productId,
    productTitle: item.productTitle,
    productNameFa: '',
    productImage: null,
    label,
    unitPrice: item.unitPrice,
    compareAtPrice: item.compareAtPrice,
    quantity: item.quantity,
    stockQuantity: null,
  }
}

export const RESUME_SHOPPING_CARD_KEY = 'meshkee:resumeShoppingCard'

export function shoppingCardToDraftItems(card: ShoppingCard): DraftCartItem[] {
  return card.items
    .map((item) => shoppingCardItemToDraft(item))
    .filter((item): item is DraftCartItem => item !== null)
}
