import { http } from "@/utils/http";
import { uploadToOss, type UploadResult } from "@/utils/ossUpload";

type Result = {
  code: number;
  message: string;
  data?: any;
};

type ResultTable = {
  code: number;
  message: string;
  data?: {
    list: Array<any>;
    total: number;
    pageSize: number;
    currentPage: number;
  };
};

// ============ OSS 上传相关 ============

/** 获取 OSS 上传凭证 */
export function getOssUploadCredential(dir: string) {
  return http.request<Result>("get", "/oss/upload-credential", {
    params: { dir }
  });
}

/** 前端直传 OSS */
export { uploadToOss };
export type { UploadResult };

/** 删除 OSS 文件 */
export function deleteOssFile(objectKey: string) {
  return http.request<Result>("delete", "/oss/file", {
    params: { objectKey }
  });
}

// ============ 门店档案 CRUD ============

/** 分页查询门店档案列表 */
export function getArchiveList(params: {
  page?: number;
  pageSize?: number;
  shopName?: string;
  status?: number;
}) {
  return http.request<ResultTable>("get", "/shop-archive/list", { params });
}

/** 查询门店档案详情 */
export function getArchiveDetail(id: string | number) {
  return http.request<Result>("get", `/shop-archive/${id}`);
}

/** 根据门店ID查询档案 */
export function getArchiveByShopId(shopId: string | number) {
  return http.request<Result>("get", `/shop-archive/by-shop/${shopId}`);
}

/** 创建门店档案 */
export function createArchive(data: any) {
  return http.request<Result>("post", "/shop-archive", { data });
}

/** 更新门店档案 */
export function updateArchive(id: string | number, data: any) {
  return http.request<Result>("put", `/shop-archive/${id}`, { data });
}

/** 删除门店档案 */
export function deleteArchive(id: string | number) {
  return http.request<Result>("delete", `/shop-archive/${id}`);
}

/** 获取枚举选项 */
export function getEnumOptions(enumType: string) {
  return http.request<Result>("get", "/shop-archive/enum-options", {
    params: { enumType }
  });
}
