import { defineStore } from "pinia";
import type { CreateMemberViewModel, UpdateMemberViewModel } from "@/viewmodels/admin/member.models";
import { http } from "@/serverCom";
import type { AxiosResponse } from "axios";
import type { MemberViewModel } from "@/viewmodels/admin/member.models";

export const useMemberStore = defineStore("member", {
  state: () => {
    return {
      members: [] as Array<MemberViewModel & { tab_pos: number }>,
      totalCount: 0 as number,
      loading: "loading" as "loading" | "fetched" | "failed",
      activeMember: null as number | null,
      activeMemberObj: null as MemberViewModel | null,
      loadingActive: "loading" as "loading" | "fetched" | "failed",
    };
  },
  actions: {
    fetchMembers(offset = 0, count = 25, clear = false) {
      if (clear) this.members = [];
      this.loading = "loading";
      http
        .get(`/admin/member?offset=${offset}&count=${count}`)
        .then((result) => {
          this.totalCount = result.data.total;
          result.data.members
            .filter((elem: MemberViewModel) => this.members.findIndex((m) => m.id == elem.id) == -1)
            .map((elem: MemberViewModel, index: number): MemberViewModel & { tab_pos: number } => {
              return {
                ...elem,
                tab_pos: index + offset,
              };
            })
            .forEach((elem: MemberViewModel & { tab_pos: number }) => {
              this.members.push(elem);
            });
          this.loading = "fetched";
        })
        .catch((err) => {
          this.loading = "failed";
        });
    },
    fetchMemberByActiveId() {
      this.loadingActive = "loading";
      http
        .get(`/admin/member/${this.activeMember}`)
        .then((res) => {
          this.activeMemberObj = res.data;
          this.loadingActive = "fetched";
        })
        .catch((err) => {
          this.loadingActive = "failed";
        });
    },
    fetchMemberById(id: number) {
      return http.get(`/admin/member/${id}`);
    },
    async createMember(member: CreateMemberViewModel): Promise<AxiosResponse<any, any>> {
      const result = await http.post(`/admin/member`, {
        salutation: member.salutation,
        firstname: member.firstname,
        lastname: member.lastname,
        nameaffix: member.nameaffix,
        birthdate: member.birthdate,
        internalId: member.internalId,
      });
      this.fetchMembers();
      return result;
    },
    async updateActiveMember(member: UpdateMemberViewModel): Promise<AxiosResponse<any, any>> {
      const result = await http.patch(`/admin/member/${member.id}`, {
        salutation: member.salutation,
        firstname: member.firstname,
        lastname: member.lastname,
        nameaffix: member.nameaffix,
        birthdate: member.birthdate,
        internalId: member.internalId,
      });
      this.fetchMembers();
      return result;
    },
    async deleteMember(member: number): Promise<AxiosResponse<any, any>> {
      const result = await http.delete(`/admin/member/${member}`);
      this.fetchMembers();
      return result;
    },
  },
});
