import { apiClient } from "@/lib/api/client";

export interface PaginatedResponse { success: boolean; data: any[]; totalCount: number; page: number; pageSize: number; }

export async function getWorkInstructionList(params?: Record<string, any>) {
  const res = await apiClient.get("/work-instruction/list", { params });
  return res.data as { success: boolean; data: any[] };
}

export async function previewWorkInstructionNo() {
  const res = await apiClient.get("/work-instruction/preview-no");
  return res.data as { success: boolean; instructionNo: string };
}

export async function saveWorkInstruction(data: any) {
  const res = await apiClient.post("/work-instruction/save", data);
  return res.data as { success: boolean; data?: any; message?: string };
}

export async function deleteWorkInstructions(ids: string[]) {
  const res = await apiClient.post("/work-instruction/delete", { ids });
  return res.data as { success: boolean; deletedCount?: number; message?: string };
}

export async function getWIItemSource(params?: Record<string, any>) {
  const res = await apiClient.get("/work-instruction/source/item", { params });
  return res.data as PaginatedResponse;
}

export async function getWISalesOrderSource(params?: Record<string, any>) {
  const res = await apiClient.get("/work-instruction/source/sales-order", { params });
  return res.data as PaginatedResponse;
}

export async function getWIProductionPlanSource(params?: Record<string, any>) {
  const res = await apiClient.get("/work-instruction/source/production-plan", { params });
  return res.data as PaginatedResponse;
}

export async function getEquipmentList() {
  const res = await apiClient.get("/work-instruction/equipment");
  return res.data as { success: boolean; data: { id: string; equipment_code: string; equipment_name: string }[] };
}

export async function getEmployeeList() {
  const res = await apiClient.get("/work-instruction/employees");
  return res.data as { success: boolean; data: { user_id: string; user_name: string; dept_name: string | null }[] };
}

// ─── 라우팅 & 공정작업기준 API ───

export interface RoutingProcess {
  routing_detail_id: string;
  seq_no: string;
  process_code: string;
  process_name: string;
  is_required?: string;
  work_type?: string;
}

export interface RoutingVersionData {
  id: string;
  version_name: string;
  description?: string;
  is_default: boolean;
  processes: RoutingProcess[];
}

export interface WIWorkItemDetail {
  id?: string;
  work_item_id?: string;
  detail_type?: string;
  content?: string;
  is_required?: string;
  sort_order?: number;
  remark?: string;
  inspection_code?: string;
  inspection_method?: string;
  unit?: string;
  lower_limit?: string;
  upper_limit?: string;
  duration_minutes?: number;
  input_type?: string;
  lookup_target?: string;
  display_fields?: string;
}

export interface WIWorkItem {
  id?: string;
  routing_detail_id?: string;
  work_phase: string;
  title: string;
  is_required: string;
  sort_order: number;
  description?: string;
  detail_count?: number;
  details?: WIWorkItemDetail[];
  source_work_item_id?: string;
}

export interface WIWorkStandardProcess {
  routing_detail_id: string;
  seq_no: string;
  process_code: string;
  process_name: string;
  workItems: WIWorkItem[];
}

export async function getRoutingVersions(wiNo: string, itemCode: string) {
  const res = await apiClient.get(`/work-instruction/${wiNo}/routing-versions/${encodeURIComponent(itemCode)}`);
  return res.data as { success: boolean; data: RoutingVersionData[] };
}

export async function updateWIRouting(wiNo: string, routingVersionId: string) {
  const res = await apiClient.put(`/work-instruction/${wiNo}/routing`, { routingVersionId });
  return res.data as { success: boolean };
}

export async function getWIWorkStandard(wiNo: string, routingVersionId: string) {
  const res = await apiClient.get(`/work-instruction/${wiNo}/work-standard`, { params: { routingVersionId } });
  return res.data as { success: boolean; data: { processes: WIWorkStandardProcess[]; isCustom: boolean } };
}

export async function copyWorkStandard(wiNo: string, routingVersionId: string) {
  const res = await apiClient.post(`/work-instruction/${wiNo}/work-standard/copy`, { routingVersionId });
  return res.data as { success: boolean };
}

export async function saveWIWorkStandard(wiNo: string, routingDetailId: string, workItems: WIWorkItem[]) {
  const res = await apiClient.put(`/work-instruction/${wiNo}/work-standard/save`, { routingDetailId, workItems });
  return res.data as { success: boolean };
}

export async function resetWIWorkStandard(wiNo: string) {
  const res = await apiClient.delete(`/work-instruction/${wiNo}/work-standard/reset`);
  return res.data as { success: boolean };
}
