import apiClient from './client'
import type {
  StepCategory,
  StepCategoryListItem,
  StepCategoryCreate,
  StepCategoryUpdate
} from '@/types'

export interface StepCategoryListParams {
  include_inactive?: boolean
  account_only?: boolean
}

export const stepCategoriesApi = {
  async list(params?: StepCategoryListParams): Promise<StepCategoryListItem[]> {
    const response = await apiClient.get<StepCategoryListItem[]>('/step-categories', { params })
    return response.data
  },

  async get(id: string): Promise<StepCategory> {
    const response = await apiClient.get<StepCategory>(`/step-categories/${id}`)
    return response.data
  },

  async create(data: StepCategoryCreate): Promise<StepCategory> {
    const response = await apiClient.post<StepCategory>('/step-categories', data)
    return response.data
  },

  async update(id: string, data: StepCategoryUpdate): Promise<StepCategory> {
    const response = await apiClient.put<StepCategory>(`/step-categories/${id}`, data)
    return response.data
  },

  async delete(id: string): Promise<void> {
    await apiClient.delete(`/step-categories/${id}`)
  },

  async archive(id: string): Promise<StepCategory> {
    const response = await apiClient.put<StepCategory>(`/step-categories/${id}`, {
      is_active: false
    })
    return response.data
  },

  async restore(id: string): Promise<StepCategory> {
    const response = await apiClient.put<StepCategory>(`/step-categories/${id}`, {
      is_active: true
    })
    return response.data
  },

  async updateOrder(updates: Array<{ id: string; display_order: number }>): Promise<void> {
    // Update display_order for multiple categories
    await Promise.all(
      updates.map(({ id, display_order }) =>
        apiClient.put(`/step-categories/${id}`, { display_order })
      )
    )
  }
}

export default stepCategoriesApi
