import { apiClient, ApiResponse } from "./client";

// ============================================
// 설계의뢰/설변요청 (DR/ECR)
// ============================================

export async function getDesignRequestList(params?: {
  source_type?: string;
  status?: string;
  priority?: string;
  search?: string;
}): Promise<ApiResponse<any[]>> {
  try {
    const res = await apiClient.get("/design/requests", { params });
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function getDesignRequestDetail(id: string): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.get(`/design/requests/${id}`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function createDesignRequest(data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post("/design/requests", data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function updateDesignRequest(id: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.put(`/design/requests/${id}`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function deleteDesignRequest(id: string): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.delete(`/design/requests/${id}`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function addRequestHistory(id: string, data: { step: string; history_date: string; user_name: string; description: string }): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post(`/design/requests/${id}/history`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

// ============================================
// 설계 프로젝트
// ============================================

export async function getProjectList(params?: {
  status?: string;
  search?: string;
}): Promise<ApiResponse<any[]>> {
  try {
    const res = await apiClient.get("/design/projects", { params });
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function getProjectDetail(id: string): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.get(`/design/projects/${id}`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function createProject(data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post("/design/projects", data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function updateProject(id: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.put(`/design/projects/${id}`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function deleteProject(id: string): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.delete(`/design/projects/${id}`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

// ============================================
// 프로젝트 태스크
// ============================================

export async function getTasksByProject(projectId: string): Promise<ApiResponse<any[]>> {
  try {
    const res = await apiClient.get(`/design/projects/${projectId}/tasks`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function createTask(projectId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post(`/design/projects/${projectId}/tasks`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function updateTask(taskId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.put(`/design/tasks/${taskId}`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function deleteTask(taskId: string): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.delete(`/design/tasks/${taskId}`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

// ============================================
// 작업일지
// ============================================

export async function getWorkLogsByTask(taskId: string): Promise<ApiResponse<any[]>> {
  try {
    const res = await apiClient.get(`/design/tasks/${taskId}/work-logs`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function createWorkLog(taskId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post(`/design/tasks/${taskId}/work-logs`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function deleteWorkLog(workLogId: string): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.delete(`/design/work-logs/${workLogId}`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

// ============================================
// 태스크 하위항목
// ============================================

export async function createSubItem(taskId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post(`/design/tasks/${taskId}/sub-items`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function updateSubItem(subItemId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.put(`/design/sub-items/${subItemId}`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function deleteSubItem(subItemId: string): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.delete(`/design/sub-items/${subItemId}`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

// ============================================
// 태스크 이슈
// ============================================

export async function createIssue(taskId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post(`/design/tasks/${taskId}/issues`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function updateIssue(issueId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.put(`/design/issues/${issueId}`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

// ============================================
// ECN (설변통보)
// ============================================

export async function getEcnList(params?: {
  status?: string;
  search?: string;
}): Promise<ApiResponse<any[]>> {
  try {
    const res = await apiClient.get("/design/ecn", { params });
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function createEcn(data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post("/design/ecn", data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function updateEcn(id: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.put(`/design/ecn/${id}`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function deleteEcn(id: string): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.delete(`/design/ecn/${id}`);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

// ============================================
// 나의 업무 (My Work)
// ============================================

export async function getMyWork(params?: {
  status?: string;
  project_id?: string;
}): Promise<ApiResponse<any[]>> {
  try {
    const res = await apiClient.get("/design/my-work", { params });
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

// ============================================
// 구매요청 / 협업요청
// ============================================

export async function createPurchaseReq(workLogId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post(`/design/work-logs/${workLogId}/purchase-reqs`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function createCoopReq(workLogId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post(`/design/work-logs/${workLogId}/coop-reqs`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}

export async function addCoopResponse(coopReqId: string, data: any): Promise<ApiResponse<any>> {
  try {
    const res = await apiClient.post(`/design/coop-reqs/${coopReqId}/responses`, data);
    return res.data;
  } catch (e: any) {
    return { success: false, message: e.message };
  }
}
