import axios from 'axios';
import qs from 'query-string';
import type { CommonPageResult, CommonStrIdParams } from './common';
import type { Pagination } from '@/types/global';
import { handleDownLoadFile } from './utils';
import type {
  KamiApiCardInfoWalmartV1AccountUpdateReq,
  KamiApiCardInfoWalmartV1AccountCreateReq,
  KamiApiCardInfoWalmartV1AccountCookieBatchInfo,
  KamiInternalModelEntityV1CardRedeemAccountHistory,
  KamiApiCardInfoWalmartV1AccountCookieCheckRes,
  KamiApiCardInfoWalmartV1AccountListRecord
} from './generated';

export interface walmartCardAddRecord {
  name: string;
  cookie: string;
  status: number;
  maxAmountLimit: number;
  wsKey?: string;
  remark?: string;
  createdUserName?: string;
}

export interface walmartCardUpdateRecord
  extends walmartCardAddRecord,
    CommonStrIdParams {}

export interface walmartCardRecord extends walmartCardUpdateRecord {
  status: 0 | 1 | 2 | 3; // 账号状态
  nickname: string;
}

export interface WalmartCardParams
  extends Partial<walmartCardRecord>,
    Partial<CommonStrIdParams>,
    Pagination {
  groupId?: number;
}

export function addWalmartCard(data: KamiApiCardInfoWalmartV1AccountCreateReq) {
  return axios.post('/api/cardInfo/walmart/account/create', data);
}

export function updateWalmartCard(
  data: KamiApiCardInfoWalmartV1AccountUpdateReq
) {
  return axios.put('/api/cardInfo/walmart/account/update', data);
}

export function deleteWalmartCard(params: CommonStrIdParams) {
  return axios.delete('/api/cardInfo/walmart/account/delete', { params });
}

export function updateWalmartCardStatus(data: { id: string; status: number }) {
  return axios.put('/api/cardInfo/walmart/account/updateStatus', data);
}

// 获取苹果账号列表
export function queryWalmartCardList(params: WalmartCardParams) {
  return axios.get<CommonPageResult<KamiApiCardInfoWalmartV1AccountListRecord>>(
    '/api/cardInfo/walmart/account/getList',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

export interface WalmartAccountWalletParams extends Pagination {
  accountId?: string;
}

// 获取账号名歘
export function queryWalmartAccountWalletList(
  params: WalmartAccountWalletParams
) {
  return axios.get<
    CommonPageResult<KamiInternalModelEntityV1CardRedeemAccountHistory>
  >('/api/cardInfo/walmart/account/getWalletList', {
    params,
    paramsSerializer: obj => {
      return qs.stringify(obj);
    }
  });
}

// 刷新账号当前状态
export function refreshWalmartCardStatus(data: CommonStrIdParams) {
  return axios.post('/api/cardInfo/walmart/account/refreshStatus', data);
}

// 下载苹果cookie数据
export async function downloadWalmartCardData() {
  const response = await axios.get(
    '/api/cardInfo/walmart/account/downloadTemplate',
    {
      responseType: 'blob'
    }
  );
  handleDownLoadFile(response.data, '沃尔玛账号批量导入模板.xlsx');
}

export function batchAdd(data: {
  list: Array<KamiApiCardInfoWalmartV1AccountCookieBatchInfo>;
}) {
  return axios.post('/api/cardInfo/walmart/account/batchAdd', data);
}

export async function downloadDataList() {
  const response = await axios.get('/api/cardInfo/walmart/account/download', {
    responseType: 'blob'
  });
  handleDownLoadFile(response.data, '沃尔玛账号下载数据.xlsx');
}

export function detectCookie(data: { cookie: string }) {
  return axios.post<KamiApiCardInfoWalmartV1AccountCookieCheckRes>(
    '/api/cardInfo/walmart/account/checkCookie',
    data
  );
}
