import { defineStore } from 'pinia'
import { ref, computed, watch } from 'vue'

const STORAGE_KEY = 'gigafibre_cart'

function loadFromStorage () {
  try {
    const raw = localStorage.getItem(STORAGE_KEY)
    return raw ? JSON.parse(raw) : []
  } catch { return [] }
}

export const useCartStore = defineStore('cart', () => {
  const items = ref(loadFromStorage())
  const taxRate = 0.14975 // TPS 5% + TVQ 9.975%

  // Persist to localStorage on every change
  watch(items, (val) => {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(val))
  }, { deep: true })

  function addItem (product) {
    const existing = items.value.find(i => i.item_code === product.item_code)
    if (existing) {
      existing.qty += 1
    } else {
      items.value.push({
        item_code: product.item_code,
        item_name: product.item_name,
        rate: product.rate,
        qty: 1,
        billing_type: product.billing_type,
        service_category: product.service_category,
        requires_visit: product.requires_visit || false,
        project_template_id: product.project_template_id || null,
        description: product.description || '',
      })
    }
  }

  function removeItem (index) {
    items.value.splice(index, 1)
  }

  function updateQty (index, qty) {
    if (qty < 1) {
      removeItem(index)
      return
    }
    items.value[index].qty = qty
  }

  function clearCart () {
    items.value = []
  }

  const itemCount = computed(() =>
    items.value.reduce((sum, i) => sum + i.qty, 0),
  )

  const onetimeTotal = computed(() =>
    items.value
      .filter(i => i.billing_type !== 'Mensuel')
      .reduce((sum, i) => sum + i.rate * i.qty, 0),
  )

  const recurringTotal = computed(() =>
    items.value
      .filter(i => i.billing_type === 'Mensuel')
      .reduce((sum, i) => sum + i.rate * i.qty, 0),
  )

  const subtotal = computed(() => onetimeTotal.value + recurringTotal.value)

  const taxAmount = computed(() => subtotal.value * taxRate)

  const grandTotal = computed(() => subtotal.value + taxAmount.value)

  const requiresVisit = computed(() =>
    items.value.some(i => i.requires_visit),
  )

  return {
    items,
    taxRate,
    addItem,
    removeItem,
    updateQty,
    clearCart,
    itemCount,
    onetimeTotal,
    recurringTotal,
    subtotal,
    taxAmount,
    grandTotal,
    requiresVisit,
  }
})
