import type { PageParam, PageResult } from '@vben/request';

import { requestClient } from '#/api/request';

export namespace EnergyAccountApi {
  export interface Account {
    id?: number;
    customerId?: number;
    customerName?: string;
    balance?: number;
    initBalance?: number;
    accumulatedRecharge?: number;
    accumulatedConsume?: number;
    reminderThreshold?: number;
    accountStatus?: number;
    lastRechargeDate?: string;
    version?: number;
    createTime?: string;
  }

  export interface AccountSimple {
    id: number;
    customerId: number;
    customerName: string;
  }

  export interface Summary {
    totalCount: number;
    totalBalance: number;
    totalRecharge: number;
    warningCount: number;
  }

  export interface ProjectAccount {
    id?: number;
    accountId?: number;
    contractId?: number;
    projectName?: string;
    contractCode?: string;
    balance?: number;
    accumulatedRecharge?: number;
    accumulatedConsume?: number;
  }

  export interface Flow {
    id?: number;
    accountId?: number;
    flowType?: number;
    bizType?: number;
    amount?: number;
    balanceBefore?: number;
    balanceAfter?: number;
    bizId?: number;
    bizCode?: string;
    remark?: string;
    operatorId?: number;
    createTime?: string;
  }
}

export function getAccountPage(params: PageParam) {
  return requestClient.get<PageResult<EnergyAccountApi.Account>>(
    '/energy/account/page',
    { params },
  );
}

export function getAccount(id: number) {
  return requestClient.get<EnergyAccountApi.Account>(
    '/energy/account/get',
    { params: { id } },
  );
}

export function getAccountSummary() {
  return requestClient.get<EnergyAccountApi.Summary>('/energy/account/summary');
}

export function getAccountSimpleList() {
  return requestClient.get<EnergyAccountApi.AccountSimple[]>('/energy/account/simple-list');
}

export function rechargeAccount(customerId: number, amount: number, remark?: string) {
  return requestClient.post('/energy/account/recharge', null, {
    params: { customerId, amount, remark },
  });
}

export function updateThreshold(id: number, threshold: number) {
  return requestClient.put('/energy/account/update-threshold', null, {
    params: { id, threshold },
  });
}

export function getProjectAccountList(accountId: number) {
  return requestClient.get<EnergyAccountApi.ProjectAccount[]>(
    '/energy/account/project/list',
    { params: { accountId } },
  );
}

export function getFlowPage(params: PageParam) {
  return requestClient.get<PageResult<EnergyAccountApi.Flow>>(
    '/energy/account/flow/page',
    { params },
  );
}

export function exportAccount(params: any) {
  return requestClient.download('/energy/account/export-excel', { params });
}
