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

import type { EnergyHydrogenDetailApi } from './hydrogen-detail';

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

export namespace EnergyBillApi {
  export interface Bill {
    id?: number;
    billCode?: string;
    energyType?: number;
    customerId?: number;
    customerName?: string;
    contractId?: number;
    stationId?: number;
    stationName?: string;
    cooperationType?: number;
    billPeriodStart?: string;
    billPeriodEnd?: string;
    receivableAmount?: number;
    actualAmount?: number;
    adjustmentAmount?: number;
    paidAmount?: number;
    totalQuantity?: number;
    detailCount?: number;
    status?: number;
    auditStatus?: number;
    submitStatus?: number;
    paymentStatus?: number;
    auditRemark?: string;
    auditTime?: string;
    generateTime?: string;
    createTime?: string;
  }

  export interface GenerateReq {
    customerId: number;
    contractId: number;
    stationId: number;
    billPeriodStart: string;
    billPeriodEnd: string;
    energyType?: number;
  }

  export interface Adjustment {
    id?: number;
    billId?: number;
    detailId?: number;
    adjustmentType?: number;
    amount?: number;
    reason?: string;
    attachmentUrls?: string;
    operatorId?: number;
    operatorName?: string;
    operateTime?: string;
    createTime?: string;
  }
}

export function getBillPage(params: PageParam) {
  return requestClient.get<PageResult<EnergyBillApi.Bill>>(
    '/energy/bill/page',
    { params },
  );
}

export function getBill(id: number) {
  return requestClient.get<EnergyBillApi.Bill>(
    '/energy/bill/get',
    { params: { id } },
  );
}

export function generateBill(data: EnergyBillApi.GenerateReq) {
  return requestClient.post<number>('/energy/bill/generate', data);
}

export function batchGenerateByPeriod(billPeriod: string) {
  return requestClient.post<Record<string, number>>(
    '/energy/bill/batch-generate-by-period',
    null,
    { params: { billPeriod } },
  );
}

export function updateBill(data: any) {
  return requestClient.put('/energy/bill/update', data);
}

export function auditBill(id: number, approved: boolean, remark?: string) {
  return requestClient.post('/energy/bill/audit', null, {
    params: { id, approved, remark },
  });
}

export function deleteBill(id: number) {
  return requestClient.delete('/energy/bill/delete', { params: { id } });
}

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

export function getBillDetailList(billId: number) {
  return requestClient.get<EnergyHydrogenDetailApi.Detail[]>(
    '/energy/bill/detail-list',
    { params: { billId } },
  );
}

export function getAdjustmentList(billId: number) {
  return requestClient.get<EnergyBillApi.Adjustment[]>(
    '/energy/bill/adjustment/list',
    { params: { billId } },
  );
}

export function createAdjustment(data: EnergyBillApi.Adjustment) {
  return requestClient.post<number>('/energy/bill/adjustment/create', data);
}

export function deleteAdjustment(id: number) {
  return requestClient.delete('/energy/bill/adjustment/delete', { params: { id } });
}
