import { apiClient } from './api-client'

const WP_URL = import.meta.env.VITE_WP_API_URL || 'https://panel.profbody.uz/wp-json/wp/v2'

export interface WpCategory {
  id: number
  count: number
  description: string
  name: string
  slug: string
  parent: number
  link?: string
}

export const wordpressCategoryService = {
  async getCategories(params: Record<string, any> = {}): Promise<WpCategory[]> {
    const query = new URLSearchParams({
      per_page: '100',
      ...params
    }).toString()

    return apiClient.fetch<WpCategory[]>(`${WP_URL}/categories?${query}`)
  },

  async getSubCategories(parentId: number): Promise<WpCategory[]> {
    return this.getCategories({ parent: parentId, orderby: 'name', order: 'asc' })
  },

  async getCategoryBySlug(slug: string): Promise<WpCategory | null> {
    const categories = await this.getCategories({ slug })
    return categories.length > 0 ? categories[0] : null
  }
}
