import type * as T from './type'
import http from '@/utils/http'

export type * from './type'

const BASE_URL = '/project-member'

// ==================== 项目看板相关接口 ====================

/** @desc 获取项目看板统计数据 */
export function getProjectKanbanStats() {
  return http.get<T.ProjectKanbanStats>(`${BASE_URL}/kanban/stats`)
}

/** @desc 获取项目看板数据 */
export function getProjectKanbanData() {
  return http.get<T.ProjectKanbanData>(`${BASE_URL}/kanban/data`)
}

/** @desc 获取项目详情 */
export function getProjectDetail(id: string | number) {
  return http.get<T.ProjectDetailResp>(`${BASE_URL}/project/${id}/detail`)
}

// ==================== 团队成员管理 ====================

/** @desc 获取项目团队成员列表（支持筛选、分页、搜索） */
export function getProjectTeamMembers(params: T.TeamMemberQuery) {
  const { projectId, ...queryParams } = params
  return http.get<T.BackendTeamMemberResp[]>(`${BASE_URL}/project/${projectId}/team-members`, { params: queryParams })
}

/** @desc 创建团队成员 */
export function createTeamMember(data: T.CreateTeamMemberForm) {
  return http.post<T.TeamMemberResp>(`${BASE_URL}/team-member`, data)
}

/** @desc 更新团队成员信息（支持更新状态） */
export function updateTeamMember(id: string | number, data: T.UpdateTeamMemberForm) {
  return http.put<T.TeamMemberResp>(`${BASE_URL}/team-member/${id}`, data)
}

/** @desc 删除团队成员（支持单个或批量删除） */
export function deleteTeamMembers(ids: string | number | (string | number)[]) {
  const idArray = Array.isArray(ids) ? ids : [ids]
  return http.del(`${BASE_URL}/team-member/batch`, { data: { ids: idArray } })
}

/** @desc 导入团队成员数据 */
export function importTeamMembers(projectId: string | number, file: File) {
  const formData = new FormData()
  formData.append('file', file)
  formData.append('projectId', projectId.toString())
  return http.post(`${BASE_URL}/team-member/import`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/** @desc 导出团队成员数据 */
export function exportTeamMembers(params: T.TeamMemberExportQuery) {
  return http.get(`${BASE_URL}/team-member/export`, { 
    params,
    responseType: 'blob'
  })
}

/** @desc 下载导入模板 */
export function downloadImportTemplate() {
  return http.get(`${BASE_URL}/team-member/template`, {
    responseType: 'blob'
  })
}

// ==================== 项目需求管理 ====================

/** @desc 获取项目需求列表 */
export function getProjectRequirements(projectId: string | number) {
  return http.get<T.ProjectRequirementResp[]>(`${BASE_URL}/project/${projectId}/requirements`)
}

/** @desc 发布项目需求 */
export function createProjectRequirement(data: T.CreateProjectRequirementForm) {
  return http.post(`${BASE_URL}/requirement`, data)
}

/** @desc 更新项目需求状态 */
export function updateRequirementStatus(data: T.UpdateRequirementStatusForm) {
  return http.patch(`${BASE_URL}/requirement/status`, data)
}

/** @desc 删除项目需求 */
export function deleteProjectRequirement(requirementId: string | number) {
  return http.del(`${BASE_URL}/requirement/${requirementId}`)
}

 