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

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

export namespace EnergyHydrogenRecordApi {
  export interface Record {
    id?: number;
    stationId?: number;
    stationName?: string;
    plateNumber?: string;
    hydrogenDate?: string;
    hydrogenQuantity?: number;
    unitPrice?: number;
    amount?: number;
    mileage?: number;
    sourceType?: number;
    matchStatus?: number;
    vehicleId?: number;
    customerId?: number;
    uploadBatchNo?: string;
    createTime?: string;
  }

  export interface ImportPreview {
    batchNo: string;
    totalCount: number;
    validCount: number;
    duplicateCount: number;
    errorCount: number;
    records: RecordPreviewItem[];
    duplicates: RecordPreviewItem[];
    errors: ImportErrorItem[];
  }

  export interface RecordPreviewItem {
    rowNum: number;
    plateNumber: string;
    hydrogenDate: string;
    hydrogenQuantity: number;
    unitPrice: number;
    amount: number;
    mileage: number;
    isDuplicate: boolean;
  }

  export interface ImportErrorItem {
    rowNum: number;
    reason: string;
  }

  export interface ImportProgress {
    current: number;
    total: number;
    status: string;
  }
}

export function getHydrogenRecordPage(params: PageParam) {
  return requestClient.get<PageResult<EnergyHydrogenRecordApi.Record>>(
    '/energy/hydrogen-record/page',
    { params },
  );
}

export function getHydrogenRecord(id: number) {
  return requestClient.get<EnergyHydrogenRecordApi.Record>(
    '/energy/hydrogen-record/get',
    { params: { id } },
  );
}

export function createHydrogenRecord(data: EnergyHydrogenRecordApi.Record) {
  return requestClient.post('/energy/hydrogen-record/create', data);
}

export function updateHydrogenRecord(data: EnergyHydrogenRecordApi.Record) {
  return requestClient.put('/energy/hydrogen-record/update', data);
}

export function deleteHydrogenRecord(id: number) {
  return requestClient.delete('/energy/hydrogen-record/delete', { params: { id } });
}

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

export function importPreview(stationId: number, file: File) {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('stationId', String(stationId));
  return requestClient.post<EnergyHydrogenRecordApi.ImportPreview>(
    '/energy/hydrogen-record/import-preview',
    formData,
  );
}

export function importConfirm(batchNo: string, duplicateStrategy: string) {
  return requestClient.post<Record<string, number>>(
    '/energy/hydrogen-record/import-confirm',
    null,
    { params: { batchNo, duplicateStrategy } },
  );
}

export function getImportProgress(batchNo: string) {
  return requestClient.get<EnergyHydrogenRecordApi.ImportProgress>(
    '/energy/hydrogen-record/import-progress',
    { params: { batchNo } },
  );
}

export function batchMatch() {
  return requestClient.post<Record<string, number>>('/energy/hydrogen-record/batch-match');
}
