import { queryTripsList } from "@/controller/TripController";
import { create } from "zustand";

type TripsListState = {
  tripsList: Model.TripsListResponse | null;
  getTripsList: (body: Model.TripListBody) => Promise<void>;
  error: string | null;
  loading?: boolean;
};

export const useTripsList = create<TripsListState>((set) => ({
  tripsList: null,
  getTripsList: async (body: Model.TripListBody) => {
    set({ loading: true, error: null });
    try {
      const response = await queryTripsList(body);
      console.log("Trip fetching API: ", response.data.trips?.length);

      set({ tripsList: response.data ?? [], loading: false });
    } catch (error) {
      console.error("Error when fetch things: ", error);
      set({
        error: "Failed to fetch things data",
        loading: false,
        tripsList: null,
      });
    }
  },
  error: null,
  loading: false,
}));
