import db from '../db/posdb'
import client from '../api/client'

/**
 * Queue an emergency payment locally.
 * Called in Emergency Mode when the server is unreachable.
 */
export async function queueOfflinePayment({ orderId, itemIds, paymentMethod }) {
  const uuid = crypto.randomUUID()
  await db.offline_payments.add({
    uuid,
    orderId,
    itemIds,
    paymentMethod,
    offlineAt: new Date().toISOString(),
    synced: 0,
    isDuplicate: 0,
  })
  return uuid
}

/**
 * Flush all unsynced offline payments to the server.
 * Called when the server comes back online.
 * Returns a summary of { synced, duplicates, failed }.
 */
export async function flushOfflinePayments() {
  // Boolean is not a valid IndexedDB key — load all and filter in JS
  const all = await db.offline_payments.toArray()
  const pending = all.filter(p => !p.synced)
  const results = { synced: 0, duplicates: 0, failed: 0 }

  for (const payment of pending) {
    try {
      const res = await client.post(`/api/orders/${payment.orderId}/pay-offline`, {
        uuid: payment.uuid,
        item_ids: payment.itemIds,
        payment_method: payment.paymentMethod,
        offline_at: payment.offlineAt,
      })
      const isDuplicate = res.data.is_duplicate
      await db.offline_payments.update(payment.localId, {
        synced: 1,
        isDuplicate: isDuplicate ? 1 : 0,
      })
      isDuplicate ? results.duplicates++ : results.synced++
    } catch {
      results.failed++
    }
  }

  return results
}

/**
 * Count unsynced pending payments (to show badge / warning).
 */
export async function pendingPaymentCount() {
  const all = await db.offline_payments.toArray()
  return all.filter(p => !p.synced).length
}
