import { requestClient } from '#/api/request';

import type { SpreadsheetConfigJson } from '#/views/business/warehouse-drug-management/admin/config/spreadsheetDefaults';

const prefix = 'spreadsheet-table-config/';

export interface SpreadsheetTableConfigItem {
  id: number;
  admin_id: number;
  table_name: string;
  sheet_key: string;
  name: string;
  is_default: number;
  config: SpreadsheetConfigJson;
  created_at: number;
  updated_at: number;
}

export async function listSpreadsheetTableConfig(params: {
  table_name: string;
  sheet_key?: string;
}) {
  return requestClient.get<SpreadsheetTableConfigItem[]>(`${prefix}list`, {
    params,
  });
}

export async function getSpreadsheetTableConfigDetail(id: number) {
  return requestClient.get<SpreadsheetTableConfigItem>(`${prefix}detail`, {
    params: { id },
  });
}

export async function createSpreadsheetTableConfig(data: {
  table_name: string;
  sheet_key?: string;
  name: string;
  is_default?: boolean;
  config: SpreadsheetConfigJson;
}) {
  return requestClient.post<SpreadsheetTableConfigItem>(`${prefix}create`, data);
}

export async function updateSpreadsheetTableConfig(data: {
  id: number;
  name?: string;
  is_default?: boolean;
  config?: SpreadsheetConfigJson;
}) {
  return requestClient.post<SpreadsheetTableConfigItem>(`${prefix}update`, data);
}

export async function deleteSpreadsheetTableConfig(id: number) {
  return requestClient.post<boolean>(`${prefix}delete`, { id });
}
