import { apiClient } from './api'

export interface AllTaskTypesResponse {
  asset_task_types: string[]
  shot_task_types: string[]
  standard_asset_types: string[]
  standard_shot_types: string[]
  custom_asset_types: string[]
  custom_shot_types: string[]
}

export interface CustomTaskTypeCreate {
  task_type: string
  category: 'asset' | 'shot'
}

export interface CustomTaskTypeUpdate {
  old_name: string
  new_name: string
  category: 'asset' | 'shot'
}

export interface TaskTypeInUseError {
  error: string
  task_type: string
  task_count: number
  task_ids: number[]
}

export const customTaskTypeService = {
  async getAllTaskTypes(projectId: number): Promise<AllTaskTypesResponse> {
    const response = await apiClient.get(`/projects/${projectId}/custom-task-types`)
    return response.data
  },

  async addCustomTaskType(projectId: number, data: CustomTaskTypeCreate): Promise<AllTaskTypesResponse> {
    const response = await apiClient.post(`/projects/${projectId}/custom-task-types`, data)
    return response.data
  },

  async updateCustomTaskType(projectId: number, taskType: string, data: CustomTaskTypeUpdate): Promise<AllTaskTypesResponse> {
    const encodedTaskType = encodeURIComponent(taskType)
    const response = await apiClient.put(`/projects/${projectId}/custom-task-types/${encodedTaskType}`, data)
    return response.data
  },

  async deleteCustomTaskType(projectId: number, taskType: string, category: 'asset' | 'shot'): Promise<AllTaskTypesResponse> {
    const encodedTaskType = encodeURIComponent(taskType)
    const response = await apiClient.delete(`/projects/${projectId}/custom-task-types/${encodedTaskType}`, {
      params: { category }
    })
    return response.data
  }
}
