/**
 * Customer CS(고객 사후관리) API 클라이언트
 */
import { apiClient } from "./client";

export interface CsItem {
  objid: string;
  mng_number: string;
  mng_type?: string;
  mng_type_title?: string;
  product_division?: string;
  product_division_title?: string;
  project_objid?: string;
  reception_date?: string;
  reception_date_title?: string;
  event_location?: string;
  action_date?: string;
  action_date_title?: string;
  customer_name?: string;
  title: string;
  performer?: string;
  performer_title?: string;
  writer?: string;
  writer_title?: string;
  analysis?: string;
  measure?: string;
  measure_type?: string;
  measure_type_title?: string;
  measure_amount?: string;
  status?: string;
  status_title?: string;
  regdate_title?: string;
  editdate_title?: string;
}

export interface CsListResponse {
  success: boolean;
  list: CsItem[];
  pagination: { page: number; pageSize: number; total: number; totalPages: number };
  summary: { sumMeasureAmount: number };
}

export interface CsListParams {
  searchYear?: string;
  mngType?: string;
  productDivision?: string;
  customerName?: string;
  startReceptionDate?: string;
  endReceptionDate?: string;
  eventLocation?: string;
  performer?: string;
  startActionDate?: string;
  endActionDate?: string;
  measureType?: string;
  status?: string;
  page?: number;
  pageSize?: number;
}

export interface CsCategories {
  MNG_TYPE: string;
  PRODUCT_DIVISION: string;
  PERFORMER: string;
  MEASURE_TYPE: string;
  STATUS: string;
}

export const customerCsApi = {
  list: async (params: CsListParams = {}): Promise<CsListResponse> =>
    (await apiClient.get("/customer-cs", { params })).data,

  detail: async (objId: string | number): Promise<CsItem> =>
    (await apiClient.get(`/customer-cs/${objId}`)).data.data,

  merge: async (payload: Partial<CsItem> & { objId?: string | number }) =>
    (await apiClient.post("/customer-cs", payload)).data.data,

  remove: async (objIds: string[]) =>
    (await apiClient.delete("/customer-cs", { data: { objIds } })).data,

  dashboard: async (searchYear?: string) =>
    (await apiClient.get("/customer-cs/dashboard", { params: { searchYear } })).data.data as {
      summary: { total_count: number; sum_amount: number; sum_paid_amount: number; sum_free_amount: number };
      byStatus: Array<{ code_id: string; code_name: string; cnt: number }>;
      byMngType: Array<{ code_id: string; code_name: string; cnt: number }>;
      byProductDivision: Array<{ code_id: string; code_name: string; cnt: number }>;
    },

  categories: async (): Promise<CsCategories> =>
    (await apiClient.get("/customer-cs/options/categories")).data.data,

  codeOptions: async (parentCodeId: string): Promise<Array<{ value: string; label: string }>> =>
    (await apiClient.get(`/customer-cs/options/codes/${parentCodeId}`)).data.data,
};
