import { defineStore } from "pinia";
import { useUserStore } from "./user";

export const useGraneleraStore = defineStore("graneleras", {
  state: (): any => ({
    pilotos: [],
  }),

  getters: {},

  actions: {
    async getPilotos() {
      const userStore = useUserStore();
      userStore.globalLoading = true;
      try {
        const { data, success, message } = await useApiFetch<any>(
          `/supervisor/granelera`,
          {
            method: "POST",
            body: {
              action: "pilotos",
            },
          },
        );

        if (success) {
          this.pilotos = data.map((p: any) => ({
            ...p,
            nombre_completo: `${p.nombre} ${p.apellido}`,
            nombre: `${p.nombre} ${p.apellido}`, // FormSelect usa .nombre
          }));
        }
      } catch (err) {
        console.error("Error fetching pilotos:", err);
      } finally {
        userStore.globalLoading = false;
      }
    },
  },
});
