import { defineStore } from "pinia";
import type {
  CreateMembershipStatusViewModel,
  UpdateMembershipStatusViewModel,
  MembershipStatusViewModel,
} from "@/viewmodels/admin/settings/membershipStatus.models";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";

export const useMembershipStatusStore = defineStore("membershipStatus", {
  state: () => {
    return {
      membershipStatus: [] as Array<MembershipStatusViewModel>,
      loading: "loading" as "loading" | "fetched" | "failed",
    };
  },
  actions: {
    fetchMembershipStatus() {
      this.loading = "loading";
      http
        .get("/admin/membershipStatus")
        .then((result) => {
          this.membershipStatus = result.data;
          this.loading = "fetched";
        })
        .catch((err) => {
          this.loading = "failed";
        });
    },
    fetchMembershipStatusById(id: number): Promise<AxiosResponse<any, any>> {
      return http.get(`/admin/membershipStatus/${id}`);
    },
    async createMembershipStatus(membershipStatus: CreateMembershipStatusViewModel): Promise<AxiosResponse<any, any>> {
      const result = await http.post(`/admin/membershipStatus`, {
        membershipStatus: membershipStatus.status,
      });
      this.fetchMembershipStatus();
      return result;
    },
    async updateActiveMembershipStatus(
      membershipStatus: UpdateMembershipStatusViewModel
    ): Promise<AxiosResponse<any, any>> {
      const result = await http.patch(`/admin/membershipStatus/${membershipStatus.id}`, {
        membershipStatus: membershipStatus.status,
      });
      this.fetchMembershipStatus();
      return result;
    },
    async deleteMembershipStatus(membershipStatus: number): Promise<AxiosResponse<any, any>> {
      const result = await http.delete(`/admin/membershipStatus/${membershipStatus}`);
      this.fetchMembershipStatus();
      return result;
    },
  },
});
