import { apiClient } from './api'

export interface AllDepartmentsResponse {
  departments: string[]
  standard_departments: string[]
  custom_departments: string[]
}

export interface CustomDepartmentCreate {
  department: string
}

export interface CustomDepartmentUpdate {
  old_name: string
  new_name: string
}

export interface DepartmentInUseError {
  error: string
  department: string
  member_count: number
  task_count: number
}

export const departmentService = {
  async getAllDepartments(projectId: number): Promise<AllDepartmentsResponse> {
    const response = await apiClient.get(`/projects/${projectId}/departments`)
    return response.data
  },

  async addDepartment(projectId: number, data: CustomDepartmentCreate): Promise<AllDepartmentsResponse> {
    const response = await apiClient.post(`/projects/${projectId}/departments`, data)
    return response.data
  },

  async updateDepartment(projectId: number, department: string, data: CustomDepartmentUpdate): Promise<AllDepartmentsResponse> {
    const encodedDepartment = encodeURIComponent(department)
    const response = await apiClient.put(`/projects/${projectId}/departments/${encodedDepartment}`, data)
    return response.data
  },

  async deleteDepartment(projectId: number, department: string): Promise<AllDepartmentsResponse> {
    const encodedDepartment = encodeURIComponent(department)
    const response = await apiClient.delete(`/projects/${projectId}/departments/${encodedDepartment}`)
    return response.data
  }
}
