import { defineStore } from "pinia";
import type { CreateInviteViewModel, InviteViewModel } from "@/viewmodels/admin/invite.models";
import { http } from "@/serverCom";
import type { PermissionObject } from "@/types/permissionTypes";
import type { AxiosResponse } from "axios";

export const useInviteStore = defineStore("invite", {
  state: () => {
    return {
      invites: [] as Array<InviteViewModel>,
      loading: "loading" as "loading" | "fetched" | "failed",
    };
  },
  actions: {
    fetchInvites() {
      this.loading = "loading";
      http
        .get("/admin/invite")
        .then((result) => {
          this.invites = result.data;
          this.loading = "fetched";
        })
        .catch((err) => {
          this.loading = "failed";
        });
    },
    createInvite(createInvite: CreateInviteViewModel): Promise<AxiosResponse<any, any>> {
      return http.post(`/admin/invite`, {
        username: createInvite.username,
        mail: createInvite.mail,
        firstname: createInvite.firstname,
        lastname: createInvite.lastname,
      });
    },
    async deleteInvite(mail: string): Promise<AxiosResponse<any, any>> {
      const result = await http.delete(`/admin/invite/${mail}`);
      this.fetchInvites();
      return result;
    },
  },
});
