export interface PushedTaskSummary {
  id: number
  documentNo: string
  name: string
  summary: string
  orgId: number | ''
  orgName: string
  bpartnerName: string
  priorityId: string | number | ''
  priorityLabel: string
  pushedTaskAt: string | null
  created: string
  isComplete: boolean
  qtySpent: number
  requestAmt: number
  taskStatus?: string
  started?: boolean
  orderId?: number | ''
  orderDocNo?: string
  productId?: number | ''
  productName?: string
  inOutId?: number | ''
  inOutDocNo?: string
}

// The current mobile worker's own pending "pushed" tickets — shared between
// the mobile dashboard's alert banner and tasks.vue's pinned list/deep-link
// handling so they never fetch/map independently and drift.
export const usePushedTasks = () => useState<PushedTaskSummary[]>('pushedTasks', () => [])

export const fetchPushedTasks = async (): Promise<PushedTaskSummary[]> => {
  try {
    const res: any = await $fetch('/api/mobile/pushed-tasks', {
      headers: useRequestHeaders(['cookie'])
    })
    const list = res?.records || []
    usePushedTasks().value = list
    return list
  } catch {
    usePushedTasks().value = []
    return []
  }
}
