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

/**
 * websopy 特殊接口域名
 * 后端 Controller: com.gxwebsoft.app.controller.AppProductController
 * @RequestMapping("/api/app/product")，后端无 context-path
 * 完整 URL = https://websopy-api.websoft.top/api/app/product/xxx
 */
const WEBSOPY_API_BASE = 'https://websopy-api.websoft.top/api';
const BASE = `${WEBSOPY_API_BASE}/app/product`;

/**
 * 分页查询应用列表
 * GET /app/product/page
 * 支持按 tenantId 过滤（管理后台按租户查询应用）
 * 注意：分页参数为 current/size（非 page/limit）
 */
export async function pageProducts(
  params: AppProductQueryParam
): Promise<PageResult<AppProduct>> {
  const res = await request.get<ApiResult<PageResult<AppProduct>>>(
    `${BASE}/page`,
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data || { list: [], count: 0 };
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取应用详情
 * GET /app/product/detail/{id}
 */
export async function getProductDetail(id: number): Promise<AppProduct> {
  const res = await request.get<ApiResult<AppProduct>>(`${BASE}/detail/${id}`);
  if (res.data.code === 0) {
    return res.data.data as AppProduct;
  }
  return Promise.reject(new Error(res.data.message));
}
