'use client'

import { useQueryClient } from '@tanstack/react-query'
import { IBaseResponse } from '@/@core/types/api'
import { request } from '@/utils'
import { TABLES_LIST_QUERY_KEY } from './useGetTablesList'
import { TABLE_STATUS_QUERY_KEY } from './useGetTableStatus'
import { useCustomMutation } from '@/hooks/api'

export const useDeleteTable = () => {
  const queryClient = useQueryClient()

  return useCustomMutation<string, IBaseResponse<void>>(
    ['tables-delete'],
    tableId =>
      request<IBaseResponse<void>>({
        url: `/tables/${tableId}`,
        method: 'DELETE'
      }),
    {
      onSuccess: async (_, tableId) => {
        await Promise.all([
          queryClient.invalidateQueries({ queryKey: [TABLES_LIST_QUERY_KEY] }),
          queryClient.invalidateQueries({ queryKey: [TABLE_STATUS_QUERY_KEY, tableId] })
        ])
      }
    }
  )
}
