import { defineStore } from "pinia";
import { getSyncExecutions } from "src/api/syncExecution";
import { ref } from "vue";

export const syncExecutionStore = defineStore("syncExecution", () => {
  const lastSyncExecution = ref(null);

  const fetchLastSyncExecution = async () => {
    try {
      const executions = await getSyncExecutions();

      if (executions && executions.length > 0) {
        const lastSuccess = [...executions]
          .reverse()
          .find((e) => e.status === "success");

        lastSyncExecution.value = lastSuccess || null;
      }
    } catch {
      lastSyncExecution.value = null;
    }
  };

  return {
    lastSyncExecution,
    fetchLastSyncExecution,
  };
});
