import { api } from '../api'

export interface Category {
  id: string
  name: string
  nameAr?: string
  parentId?: string
  parent?: Category
  children?: Category[]
  description?: string
  isActive: boolean
  createdAt: string
  updatedAt: string
  _count?: {
    contacts: number
  }
}

export interface CreateCategoryData {
  name: string
  nameAr?: string
  parentId?: string
  description?: string
}

export interface UpdateCategoryData extends Partial<CreateCategoryData> {
  isActive?: boolean
}

export const categoriesAPI = {
  // Get all categories (flat list)
  getAll: async (): Promise<Category[]> => {
    const response = await api.get('/contacts/categories')
    return response.data.data
  },

  // Get category tree (hierarchical)
  getTree: async (): Promise<Category[]> => {
    const response = await api.get('/contacts/categories/tree')
    return response.data.data
  },

  // Get single category by ID
  getById: async (id: string): Promise<Category> => {
    const response = await api.get(`/contacts/categories/${id}`)
    return response.data.data
  },

  // Create new category
  create: async (data: CreateCategoryData): Promise<Category> => {
    const response = await api.post('/contacts/categories', data)
    return response.data.data
  },

  // Update existing category
  update: async (id: string, data: UpdateCategoryData): Promise<Category> => {
    const response = await api.put(`/contacts/categories/${id}`, data)
    return response.data.data
  },

  // Delete category
  delete: async (id: string): Promise<void> => {
    await api.delete(`/contacts/categories/${id}`)
  }
}
