import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import {ShopGift, ShopGiftParam, GiftRedeemParam, GiftUseParam, QRCodeParam} from './model';

/**
 * 分页查询礼品卡
 */
export async function pageShopGift(params: ShopGiftParam) {
  const res = await request.get<ApiResult<PageResult<ShopGift>>>(
    '/shop/shop-gift/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询礼品卡列表
 */
export async function listShopGift(params?: ShopGiftParam) {
  const res = await request.get<ApiResult<ShopGift[]>>(
    '/shop/shop-gift',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加礼品卡
 */
export async function addShopGift(data: ShopGift) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-gift',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
}

/**
 * 生成礼品卡
 */
export async function makeShopGift(data: ShopGift) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-gift/make',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改礼品卡
 */
export async function updateShopGift(data: ShopGift) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-gift',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除礼品卡
 */
export async function removeShopGift(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-gift/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 批量删除礼品卡
 */
export async function removeBatchShopGift(data: (number | undefined)[]) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-gift/batch',
    {
      data
    }
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询礼品卡
 */
export async function getShopGift(id: number) {
  const res = await request.get<ApiResult<ShopGift>>(
    '/shop/shop-gift/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据code查询礼品卡
 * @param code
 */
export async function getShopGiftByCode(code: string) {
  const res = await request.get<ApiResult<ShopGift>>(
    '/shop/shop-gift/by-code/' + code
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}



/**
 * 兑换礼品卡
 */
export async function redeemGift(params: GiftRedeemParam) {
  const res = await request.post<ApiResult<ShopGift>>(
    '/shop/shop-gift/redeem',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 使用礼品卡
 */
export async function useGift(params: GiftUseParam) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-gift/use',
    params
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 获取用户的礼品卡列表
 */
export async function getUserGifts(params: ShopGiftParam) {
  const res = await request.get<ApiResult<PageResult<ShopGift>>>(
    '/shop/shop-gift/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 验证礼品卡兑换码
 */
export async function validateGiftCode(code: string) {
  const res = await request.get<ApiResult<ShopGift>>(
    `/shop/shop-gift/validate/${code}`
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

export async function exportShopGift(ids?: number[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-gift/export',
    ids
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 生成礼品卡核销码(可用)
 */
export async function generateVerificationCode(data: QRCodeParam) {
  const res = await request.post<ApiResult<unknown>>(
    '/qr-code/create-encrypted-qr-code',
    data
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 验证核销码
 */
export async function verifyGiftCard(params: { verificationCode?: string; giftCode?: string }) {
  const res = await request.post<ApiResult<ShopGift>>(
    '/shop/shop-gift/verify',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 完成礼品卡核销
 */
export async function completeVerification(params: {
  giftId: number;
  verificationCode: string;
  storeId?: number;
  storeName?: string;
  operatorId?: number;
  operatorName?: string;
}) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-gift/complete-verification',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 解密二维码数据
 */
export async function decryptQrData(params: { token: string; encryptedData: string }) {
  const res = await request.post<ApiResult<string>>(
    '/qr-code/decrypt-qr-data',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

