'use client'

import { IBaseResponse, CustomUseQueryOptions } from '@/@core/types/api'
import { useCustomQuery } from '@/hooks/api'
import type { IOrderStatistics, ORDER_STATUS, ORDER_TYPE } from '@/types/data'
import { request } from '@/utils'

export const ORDERS_STATISTICS_QUERY_KEY = 'orders-statistics'

export type OrdersStatisticsQuery = {
  date_from?: string
  date_to?: string
  status?: ORDER_STATUS
  order_type?: ORDER_TYPE
}

export const useGetOrdersStatistics = (
  query?: OrdersStatisticsQuery,
  options?: CustomUseQueryOptions<IBaseResponse<IOrderStatistics>>
) => {
  return useCustomQuery<IBaseResponse<IOrderStatistics>>(
    [ORDERS_STATISTICS_QUERY_KEY, query],
    () => {
      const params: Record<string, string> = {}

      if (query) {
        Object.entries(query).forEach(([key, value]) => {
          if (value !== undefined && value !== null && value !== '') {
            params[key] = String(value)
          }
        })
      }

      return request({
        url: '/orders/statistics/dashboard',
        params
      })
    },
    options
  )
}
