import { apiClient } from "./client";

export interface MoldInfo {
  id: string;
  company_code: string;
  mold_code: string;
  mold_name: string;
  mold_type: string | null;
  category: string | null;
  manufacturer: string | null;
  manufacturing_number: string | null;
  manufacturing_date: string | null;
  cavity_count: number;
  shot_count: number;
  mold_quantity: number;
  base_input_qty: number;
  operation_status: string;
  remarks: string | null;
  image_path: string | null;
  memo: string | null;
  created_date: string;
  updated_date: string;
  writer: string | null;
}

export interface MoldSerial {
  id: string;
  company_code: string;
  mold_code: string;
  serial_number: string;
  status: string;
  progress: number;
  work_description: string | null;
  manager: string | null;
  completion_date: string | null;
  remarks: string | null;
  created_date: string;
}

export interface MoldInspectionItem {
  id: string;
  company_code: string;
  mold_code: string;
  inspection_item: string;
  inspection_cycle: string | null;
  inspection_method: string | null;
  inspection_content: string | null;
  lower_limit: string | null;
  upper_limit: string | null;
  unit: string | null;
  is_active: string;
  checklist: string | null;
  remarks: string | null;
  created_date: string;
}

export interface MoldPart {
  id: string;
  company_code: string;
  mold_code: string;
  part_name: string;
  replacement_cycle: string | null;
  unit: string | null;
  specification: string | null;
  manufacturer: string | null;
  manufacturer_code: string | null;
  image_path: string | null;
  remarks: string | null;
  created_date: string;
}

export interface MoldSerialSummary {
  total: number;
  in_use: number;
  repair: number;
  stored: number;
  disposed: number;
}

interface ApiResponse<T> {
  success: boolean;
  data?: T;
  message?: string;
}

const handleResponse = async <T>(promise: Promise<any>): Promise<ApiResponse<T>> => {
  try {
    const response = await promise;
    return response.data;
  } catch (error: any) {
    return {
      success: false,
      message: error.response?.data?.message || error.message || "오류가 발생했습니다.",
    };
  }
};

// 금형 마스터
export const getMoldList = (params?: Record<string, string>) =>
  handleResponse<MoldInfo[]>(apiClient.get("/mold", { params }));

export const getMoldDetail = (moldCode: string) =>
  handleResponse<MoldInfo>(apiClient.get(`/mold/${moldCode}`));

export const createMold = (data: Partial<MoldInfo>) =>
  handleResponse<MoldInfo>(apiClient.post("/mold", data));

export const updateMold = (moldCode: string, data: Partial<MoldInfo>) =>
  handleResponse<MoldInfo>(apiClient.put(`/mold/${moldCode}`, data));

export const deleteMold = (moldCode: string) =>
  handleResponse<void>(apiClient.delete(`/mold/${moldCode}`));

// 일련번호
export const getMoldSerials = (moldCode: string) =>
  handleResponse<MoldSerial[]>(apiClient.get(`/mold/${moldCode}/serials`));

export const createMoldSerial = (moldCode: string, data: Partial<MoldSerial>) =>
  handleResponse<MoldSerial>(apiClient.post(`/mold/${moldCode}/serials`, data));

export const deleteMoldSerial = (id: string) =>
  handleResponse<void>(apiClient.delete(`/mold/serials/${id}`));

export const getMoldSerialSummary = (moldCode: string) =>
  handleResponse<MoldSerialSummary>(apiClient.get(`/mold/${moldCode}/serial-summary`));

// 점검항목
export const getMoldInspections = (moldCode: string) =>
  handleResponse<MoldInspectionItem[]>(apiClient.get(`/mold/${moldCode}/inspections`));

export const createMoldInspection = (moldCode: string, data: Partial<MoldInspectionItem>) =>
  handleResponse<MoldInspectionItem>(apiClient.post(`/mold/${moldCode}/inspections`, data));

export const deleteMoldInspection = (id: string) =>
  handleResponse<void>(apiClient.delete(`/mold/inspections/${id}`));

// 부품
export const getMoldParts = (moldCode: string) =>
  handleResponse<MoldPart[]>(apiClient.get(`/mold/${moldCode}/parts`));

export const createMoldPart = (moldCode: string, data: Partial<MoldPart>) =>
  handleResponse<MoldPart>(apiClient.post(`/mold/${moldCode}/parts`, data));

export const deleteMoldPart = (id: string) =>
  handleResponse<void>(apiClient.delete(`/mold/parts/${id}`));
