import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopSetting, ShopSettingParam } from './model';

/**
 * 分页查询设置
 */
export async function pageShopSetting(params: ShopSettingParam) {
  const res = await request.get<ApiResult<PageResult<ShopSetting>>>(
    '/shop/shop-setting/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询设置列表
 */
export async function listShopSetting(params?: ShopSettingParam) {
  const res = await request.get<ApiResult<ShopSetting[]>>(
    '/shop/shop-setting',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据分类获取设置列表
 */
export async function getShopSettingByCategory(category: string) {
  const res = await request.get<ApiResult<ShopSetting[]>>(
    `/shop/shop-setting/category/${category}`
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 获取分类设置值（Map格式）
 */
export async function getShopSettingCategoryValues(category: string) {
  const res = await request.get<ApiResult<Record<string, string>>>(
    `/shop/shop-setting/category/${category}/values`
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据key获取设置值
 */
export async function getShopSettingValue(key: string) {
  const res = await request.get<ApiResult<string>>(
    `/shop/shop-setting/key/${key}`
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据key获取完整设置
 */
export async function getShopSettingByKey(key: string) {
  const res = await request.get<ApiResult<ShopSetting>>(
    `/shop/shop-setting/info/${key}`
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 保存或更新设置
 */
export async function saveShopSetting(data: ShopSetting) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-setting',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 更新设置
 */
export async function updateShopSetting(data: ShopSetting) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-setting',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量保存分类设置
 */
export async function batchSaveShopSetting(category: string, values: Record<string, unknown>) {
  const res = await request.post<ApiResult<unknown>>(
    `/shop/shop-setting/batch/${category}`,
    values
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除设置
 */
export async function removeShopSetting(settingId: number) {
  const res = await request.del<ApiResult<unknown>>(
    `/shop/shop-setting/${settingId}`
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}
