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.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

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

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

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