import { ChartOfAccountTypes } from '@/types/services/chartOfAccount'
import { useQuery } from '@tanstack/react-query'
import { api } from '../api'

interface ChartOfAccountQueryParams {
  page?: number
  limit?: number
  search?: string
}

export function useChartOfAccountTypes(params: ChartOfAccountQueryParams = {}) {
  const { page = 1, limit = 10, search = '', ...filters } = params

  return useQuery<ChartOfAccountTypes>({
    queryKey: ['chart-of-account-types', { page, limit, search, ...filters }],
    queryFn: async () => {
      const queryParams = new URLSearchParams()

      queryParams.append('page', page.toString())
      queryParams.append('limit', limit.toString())

      if (search) {
        queryParams.append('search', search)
      }

      Object.entries(filters).forEach(([key, value]) => {
        if (value !== undefined && value !== null && value !== '') {
          queryParams.append(key, value.toString())
        }
      })

      const res = await api.get(`/chart-of-account-types?${queryParams.toString()}`)
      return res.data.data
    }
  })
}

export interface AccountRequest {
  chart_of_account_id: string
  name: string
  number: string
  account_type: string
  opening_balance: number
  description: string
}
