export interface PriceInput {
  price: number | null
  specialPrice: number | null
  specialFrom: string | null
  specialTo: string | null
}

export interface PriceView extends PriceInput {
  effectivePrice: number | null
  specialActive: boolean
}

/**
 * Base vs special price, the way Magento applies it: a special price wins when today is inside its
 * (inclusive, open-ended) date window and it is lower than the base price.
 * Catalog price rules / customer-group prices are NOT included — see ARCHITECTURE.md §3.3.
 */
export function effectivePrice(p: PriceInput, today = new Date().toISOString().slice(0, 10)): PriceView {
  const inWindow = (!p.specialFrom || p.specialFrom.slice(0, 10) <= today) && (!p.specialTo || p.specialTo.slice(0, 10) >= today)
  const specialActive = p.specialPrice != null && inWindow && (p.price == null || p.specialPrice < p.price)
  return { ...p, specialActive, effectivePrice: specialActive ? p.specialPrice : p.price }
}
