import { xfetch } from '~/composables/useXfetch'

export async function create(path: string, data: any, name: string = 'item') {
  try {
    const resp = await xfetch(path, 'POST', data)
    const result: any = {}
    result.success = resp.success
    result.body = (resp.body as Record<string, any>) || {}
    return result
  } catch (error) {
    console.error(`Error posting ${name}:`, error)
    throw new Error(`Failed to post ${name}`)
  }
}

export async function getList(path: string, params: any = null, name: string = 'item') {
  try {
    let url = path
    if (params && typeof params === 'object' && Object.keys(params).length > 0) {
      const searchParams = new URLSearchParams()
      for (const key in params) {
        if (params[key] !== null && params[key] !== undefined && params[key] !== '') {
          searchParams.append(key, params[key])
        }
      }
      const queryString = searchParams.toString()
      if (queryString) url += `?${queryString}`
    }
    const resp = await xfetch(url, 'GET')
    const result: any = {}
    result.success = resp.success
    result.body = (resp.body as Record<string, any>) || {}
    return result
  } catch (error) {
    console.error(`Error fetching ${name}s:`, error)
    throw new Error(`Failed to fetch ${name}s`)
  }
}

export async function getDetail(path: string, id: number | string, name: string = 'item', params?: any) {
  try {
    let paramStr = ''
    if (params && typeof params === 'object' && Object.keys(params).length > 0) {
      const searchParams = new URLSearchParams()
      for (const key in params) {
        if (params[key] !== null && params[key] !== undefined && params[key] !== '') {
          searchParams.append(key, params[key])
        }
      }
      const queryString = searchParams.toString()
      paramStr += queryString ? `?${queryString}` : ''
    }
    const resp = await xfetch(`${path}/${id}${paramStr}`, 'GET')
    const result: any = {}
    result.success = resp.success
    result.body = (resp.body as Record<string, any>) || {}
    return result
  } catch (error) {
    console.error(`Error fetching ${name} detail:`, error)
    throw new Error(`Failed to get ${name} detail`)
  }
}

export async function update(path: string, id: number | string, data: any, name: string = 'item') {
  try {
    const resp = await xfetch(`${path}/${id}`, 'PATCH', data)
    const result: any = {}
    result.success = resp.success
    result.body = (resp.body as Record<string, any>) || {}
    return result
  } catch (error) {
    console.error(`Error putting ${name}:`, error)
    throw new Error(`Failed to put ${name}`)
  }
}

export async function remove(path: string, id: number | string, name: string = 'item') {
  try {
    const resp = await xfetch(`${path}/${id}`, 'DELETE')
    const result: any = {}
    result.success = resp.success
    result.body = (resp.body as Record<string, any>) || {}
    return result
  } catch (error) {
    console.error(`Error deleting ${name}:`, error)
    throw new Error(`Failed to delete ${name}`)
  }
}
