import { defineStore } from "pinia";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type {
  ProtocolDecisionViewModel,
  SyncProtocolDecisionViewModel,
} from "@/viewmodels/admin/club/protocol/protocolDecision.models";
import { useProtocolStore } from "./protocol";
import cloneDeep from "lodash.clonedeep";
import isEqual from "lodash.isequal";
import differenceWith from "lodash.differencewith";

export const useProtocolDecisionStore = defineStore("protocolDecision", {
  state: () => {
    return {
      initialized: false as boolean,
      decision: [] as Array<ProtocolDecisionViewModel>,
      origin: [] as Array<ProtocolDecisionViewModel>,
      loading: "loading" as "loading" | "fetched" | "failed",
      syncingProtocolDecision: "synced" as "synced" | "syncing" | "detectedChanges" | "failed",
    };
  },
  getters: {
    detectedChangeProtocolDecision: (state) =>
      !isEqual(state.origin, state.decision) && state.syncingProtocolDecision != "syncing",
  },
  actions: {
    setProtocolDecisionSyncingState(state: "synced" | "syncing" | "detectedChanges" | "failed") {
      this.syncingProtocolDecision = state;
    },
    fetchProtocolDecision() {
      this.loading = "loading";
      this.fetchProtocolDecisionPromise()
        .then((result) => {
          this.origin = result.data;
          this.decision = cloneDeep(this.origin);
          this.initialized = true;
          this.loading = "fetched";
        })
        .catch((err) => {
          console.log(err);
          this.loading = "failed";
        });
    },
    fetchProtocolDecisionPromise() {
      const protocolId = useProtocolStore().activeProtocol;
      return http.get(`/admin/protocol/${protocolId}/decisions`);
    },
    createProtocolDecision() {
      const protocolId = useProtocolStore().activeProtocol;
      if (protocolId == null) return;
      return http
        .post(`/admin/protocol/${protocolId}/decision`)
        .then((res) => {
          this.decision.push({
            id: Number(res.data),
            topic: "",
            context: "",
            protocolId: Number(protocolId),
          });
        })
        .catch((err) => {});
    },
    async synchronizeActiveProtocolDecision() {
      if (!this.initialized) return;

      this.syncingProtocolDecision = "syncing";
      const protocolId = useProtocolStore().activeProtocol;

      await http
        .patch(`/admin/protocol/${protocolId}/synchronize/decisions`, {
          decisions: differenceWith(this.decision, this.origin, isEqual),
        })
        .then((res) => {
          this.syncingProtocolDecision = "synced";
        })
        .catch((err) => {
          this.syncingProtocolDecision = "failed";
        });
      this.fetchProtocolDecisionPromise()
        .then((res) => {
          this.origin = res.data;
          if (this.detectedChangeProtocolDecision) this.syncingProtocolDecision = "detectedChanges";
        })
        .catch((err) => {});
    },
  },
});