/**
 * 岗位管理 API
 */

import { get, post, put, del } from '../request'
import type { PageParams } from '../config'

// 岗位接口
export interface Position {
  id: number
  name: string
  code: string
  parentId: number | null
  parentName?: string
  memberCount: number
  description: string
  status: 'active' | 'inactive'
  createTime: string
  children?: Position[]
}

// 岗位表单接口
export interface PositionForm {
  name: string
  code: string
  parentId: number | null
  description: string
  status: 'active' | 'inactive'
  skills?: string[]
  level?: string
}

// 获取岗位列表
export const getPositionList = (params?: PageParams & { keyword?: string }) => {
  const q: any = { ...(params || {}) }
  if ('pageSize' in q) {
    q.page_size = q.pageSize
    delete q.pageSize
  }
  return get('/api/v1/admin/positions', q)
}

// 获取岗位树形结构
export const getPositionTree = () => {
  return get('/api/v1/admin/positions/tree')
}

// 获取岗位详情
export const getPositionDetail = (id: number) => {
  return get(`/api/v1/admin/positions/${id}`)
}

// 创建岗位
export const createPosition = (data: PositionForm) => {
  return post('/api/v1/admin/positions', data)
}

// 更新岗位
export const updatePosition = (id: number, data: PositionForm) => {
  return put(`/api/v1/admin/positions/${id}`, data)
}

// 删除岗位
export const deletePosition = (id: number) => {
  return del(`/api/v1/admin/positions/${id}`)
}

// 检查岗位是否可删除
export const checkPositionDeletable = (id: number) => {
  return get(`/api/v1/admin/positions/${id}/check-delete`)
}

// 添加岗位成员
export const addPositionMembers = (positionId: number, userIds: number[]) => {
  return post(`/api/v1/admin/positions/${positionId}/members`, { user_ids: userIds })
}

// 移除岗位成员
export const removePositionMember = (positionId: number, userId: number) => {
  return del(`/api/v1/admin/positions/${positionId}/members/${userId}`)
}
