import axios from 'axios';
import qs from 'query-string';
import type { CommonPageResult } from './common';
import type { Pagination } from '@/types/global';

export interface PaymentRecord {
  id: number;
  userId: string;
  balance: string;
  totalDeposits: string;
  totalConsumption: string;
  lastDepositTime: string;
  lastConsumptionTime: string;
  totalDepositCount: number;
  totalConsumptionCount: number;
  status: string;
  notes: string;
}

export interface PaymentLogRecord extends Pagination {
  id: number;
  transactionId: string;
  paymentId: number;
  userId: string;
  amount: number;
  category: string;
  transactionType: string;
  status: string;
  notes: string;
  createdAt: string;
}

export interface PaymentLogRecordParams extends Pagination {
  userId: string;
  transactionType?: string;
}

export interface PaymentAddRecordParams {
  userId: string;
  amount: number;
  transactionType: string;
  remark: string;
}

// 获取用户钱包流水
export function queryUserPaymentList(params: PaymentLogRecordParams) {
  return axios.get<CommonPageResult<PaymentLogRecord>>(
    '/api/sysUser/payment/list',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

// 往钱包内充值
export function addUserPaymentRecord(data: PaymentAddRecordParams) {
  return axios.post<CommonPageResult<PaymentLogRecord>>(
    '/api/sysUser/payment/add',
    data
  );
}

export function queryUserPaymentRecordsList(params: PaymentLogRecordParams) {
  return axios.get<CommonPageResult<PaymentLogRecord>>(
    '/api/sysUser/paymentRecords/list',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}

export interface UserPaymentSummaryRecord {
  username: string;
  totalDepositAmount: number;
  totalConsumeAmount: number;
  totalManualHandleAmount: number;
  createdAt: string;
}

export interface UserPaymentListParams extends Pagination {
  username?: string;
}

export function queryUserPaymentSummaryList(params: UserPaymentListParams) {
  return axios.get<CommonPageResult<UserPaymentSummaryRecord>>(
    '/api/sysUser/paymentSummary/list',
    {
      params,
      paramsSerializer: obj => {
        return qs.stringify(obj);
      }
    }
  );
}
