import axiosInstance from 'api/axios';
import { PACKETS } from 'api/URL';

export interface PacketsData {
  data: {
    id: number;
    packetName: string;
    weight: number;
    totalPrice: number;
    paymentStatus: string;
    paymentType: string;
    deliveryStatus: string;
    items: {
      trekId: string;
      name: string;
      weight: number;
      price: number;
      totalPrice: number;
    }[];
    qrCode: string;
  }[];
  totalPages: number;
  totalElements: number;
}

interface Params {
  cargoType: 'AUTO' | 'AVIA';
  page: number;
  size: number;
  sort: string;
  direction: string;
}

export interface payPayload {
  payType: string;
}

const packetsApi = {
  async getPackets(params: Params): Promise<PacketsData> {
    const response = await axiosInstance.get<PacketsData>(PACKETS, { params });
    return response.data;
  },
  async payPackets(id: number, payload: payPayload) {
    try {
      const data = await axiosInstance.post(`${PACKETS}/${id}/pay`, payload);
      return data;
    } catch (error) {
      throw error;
    }
  },
  async getPacketsStatus(status: string, params: any): Promise<PacketsData> {
    const response = await axiosInstance.get<PacketsData>(
      `${PACKETS}/${status}`,
      { params },
    );
    return response.data;
  },
};

export default packetsApi;
