import { request } from "@/utils/request";

// Generic CRUD helper for asset/base/biz modules
function makeAssetApi(prefix: string) {
  return {
    page(params: Record<string, unknown>) {
      return request.post(`${prefix}/page`, params);
    },
    detail(id: string) {
      return request.get(`${prefix}/detail`, { params: { id } });
    },
    save(data: Record<string, unknown>) {
      return request.post(`${prefix}/save`, data);
    },
    del(id: string) {
      return request.post(`${prefix}/delete`, { id });
    },
  };
}

export const deviceApi = makeAssetApi("/asset/device");
export const licenseApi = makeAssetApi("/asset/license");
export const accessoryApi = makeAssetApi("/asset/accessory");
export const consumableApi = makeAssetApi("/asset/consumable");
export const depreciationApi = makeAssetApi("/asset/depreciation");
export const offeringApi = makeAssetApi("/asset/offering");

// Change record API
export const changeRecordApi = {
  page(params: Record<string, unknown>) {
    return request.post("/asset/change-record/page", params);
  },
};

// User/dept helper for finding asset manager
export const findAssetManager = (deptId?: string) => {
  return request.get("/sys/user/find_asset_manager", { params: { dept_id: deptId } });
};

// Base module list helpers (for dropdown options)
export const getModelList = () => request.get("/base/model/list");
export const getCategoryList = (categoryType?: string) => request.get("/base/category/list", { params: categoryType ? { category_type: categoryType } : undefined });
export const getLocationList = () => request.get("/base/location/list");
export const getSupplierList = () => request.get("/base/supplier/list");
export const getManufacturerList = () => request.get("/base/manufacturer/list");
export const getUserList = () => request.post("/sys/user/page", { page: 1, pageSize: 1000 });

// Base module
export const categoryApi = makeAssetApi("/base/category");
export const locationApi = makeAssetApi("/base/location");
export const manufacturerApi = makeAssetApi("/base/manufacturer");
export const modelApi = makeAssetApi("/base/model");
export const supplierApi = makeAssetApi("/base/supplier");

// Biz module
export const failureApi = makeAssetApi("/biz/failure");
export const stocktakeApi = makeAssetApi("/biz/stocktake");
export const allocationApi = makeAssetApi("/biz/allocation");
export const transactionApi = makeAssetApi("/biz/transaction");
export const requestApi = makeAssetApi("/biz/request");
