import { apiClient } from "./client";

// ─── 타입 (wace project.xml wbsTemplateMngGridList 1:1) ─────

export interface TemplateRow {
  objid: string;
  product_objid: string | null;
  product_name: string | null;        // CODE_NAME(PRODUCT_OBJID)
  title: string | null;
  writer: string | null;
  writer_title: string | null;        // DEPT_NAME || USER_NAME
  reg_date: string | null;
  reg_date_title: string | null;      // YYYY-MM-DD
  wbs_task_cnt: number | string | null;
  customer_product: string | null;
}

export interface TemplateMaster {
  objid: string;
  product_objid: string;
  product_objid_name: string;
  title: string;
  writer: string;
  reg_date: string;
  customer_product: string;
}

export interface TemplateTask {
  objid: string;
  parent_objid: string;
  task_name: string;
  task_seq: string;
  task_level: string;
  user_id: string;
  user_id_title: string;
  writer: string;
  reg_date: string;
  unit_no: string;
  upper_task_objid: string;
}

export interface TemplateDetail {
  master: TemplateMaster;
  tasks: TemplateTask[];
}

// 저장 payload — wace mergeExcelUploadWBS의 폼 hidden 직렬화에 대응
export interface WbsTaskInput {
  WBS_TASK_OBJID: string;
  TASK_NAME: string;
  UNIT_NO: string;
  UPPER_TASK_OBJID: string;
  TASK_LEVEL: string;
}

export interface SaveTemplatePayload {
  templateObjId?: string;       // 있으면 수정, 없으면 신규
  product: string;
  title: string;
  customer_product?: string;
  tasks: WbsTaskInput[];        // TOTAL 행 포함 (TASK_LEVEL=0)
}

// 엑셀 파싱 결과
export interface ParsedExcelRow {
  WBS_OBJID: string;
  UNIT_NO: string;
  TASK_NAME: string;
}

// ─── API ────────────────────────────────────────────────

export const wbsTemplateApi = {
  async list(product?: string): Promise<TemplateRow[]> {
    const res = await apiClient.get("/project/wbs-template", {
      params: product ? { product } : {},
    });
    return (res.data?.data ?? []) as TemplateRow[];
  },

  async detail(objid: string): Promise<TemplateDetail | null> {
    const res = await apiClient.get(`/project/wbs-template/${objid}`);
    return res.data?.data ?? null;
  },

  async checkDuplicate(product: string, title: string): Promise<boolean> {
    const res = await apiClient.get("/project/wbs-template/check-duplicate", {
      params: { product, title },
    });
    return Boolean(res.data?.data?.duplicate);
  },

  async save(payload: SaveTemplatePayload) {
    const res = await apiClient.post("/project/wbs-template", payload);
    return res.data;
  },

  async remove(objids: string[]) {
    const res = await apiClient.delete("/project/wbs-template", {
      data: { objids },
    });
    return res.data;
  },

  async parseExcel(file: File): Promise<ParsedExcelRow[]> {
    const form = new FormData();
    form.append("file", file);
    const res = await apiClient.post("/project/wbs-template/parse-excel", form, {
      headers: { "Content-Type": "multipart/form-data" },
    });
    return (res.data?.data ?? []) as ParsedExcelRow[];
  },
};
