import { apiClient } from "./client";
import {
  BarcodeLabelMaster,
  BarcodeLabelLayout,
  GetBarcodeLabelsParams,
  GetBarcodeLabelsResponse,
  CreateBarcodeLabelRequest,
  UpdateBarcodeLabelRequest,
} from "@/types/barcode";

const BASE_URL = "/admin/barcode-labels";

export interface BarcodeLabelTemplate {
  template_id: string;
  template_name_kor: string;
  template_name_eng: string | null;
  width_mm: number;
  height_mm: number;
  layout_json: string;
  sort_order: number;
}

export const barcodeApi = {
  /** 바코드 라벨 목록 조회 */
  getLabels: async (params: GetBarcodeLabelsParams) => {
    const response = await apiClient.get<{
      success: boolean;
      data: GetBarcodeLabelsResponse;
    }>(BASE_URL, { params });
    return response.data;
  },

  /** 바코드 라벨 상세 조회 */
  getLabelById: async (labelId: string) => {
    const response = await apiClient.get<{
      success: boolean;
      data: BarcodeLabelMaster;
    }>(`${BASE_URL}/${labelId}`);
    return response.data;
  },

  /** 라벨 레이아웃 조회 */
  getLayout: async (labelId: string) => {
    const response = await apiClient.get<{
      success: boolean;
      data: BarcodeLabelLayout;
    }>(`${BASE_URL}/${labelId}/layout`);
    return response.data;
  },

  /** 라벨 레이아웃 저장 */
  saveLayout: async (labelId: string, layout: BarcodeLabelLayout) => {
    const response = await apiClient.put<{
      success: boolean;
      message: string;
    }>(`${BASE_URL}/${labelId}/layout`, layout);
    return response.data;
  },

  /** 기본 템플릿 목록 */
  getTemplates: async () => {
    const response = await apiClient.get<{
      success: boolean;
      data: BarcodeLabelTemplate[];
    }>(`${BASE_URL}/templates`);
    return response.data;
  },

  /** 템플릿 상세 (레이아웃 적용용) */
  getTemplateById: async (templateId: string) => {
    const response = await apiClient.get<{
      success: boolean;
      data: BarcodeLabelTemplate & { layout: BarcodeLabelLayout };
    }>(`${BASE_URL}/templates/${templateId}`);
    return response.data;
  },

  /** 바코드 라벨 생성 (templateId 선택 시 해당 레이아웃 적용) */
  createLabel: async (data: CreateBarcodeLabelRequest & { templateId?: string }) => {
    const response = await apiClient.post<{
      success: boolean;
      data: { labelId: string };
      message: string;
    }>(BASE_URL, data);
    return response.data;
  },

  /** 바코드 라벨 수정 */
  updateLabel: async (labelId: string, data: UpdateBarcodeLabelRequest) => {
    const response = await apiClient.put<{
      success: boolean;
      message: string;
    }>(`${BASE_URL}/${labelId}`, data);
    return response.data;
  },

  /** 바코드 라벨 삭제 */
  deleteLabel: async (labelId: string) => {
    const response = await apiClient.delete<{
      success: boolean;
      message: string;
    }>(`${BASE_URL}/${labelId}`);
    return response.data;
  },

  /** 바코드 라벨 복사 */
  copyLabel: async (labelId: string) => {
    const response = await apiClient.post<{
      success: boolean;
      data: { labelId: string };
      message: string;
    }>(`${BASE_URL}/${labelId}/copy`);
    return response.data;
  },
};
