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

export const useCommunicationStore = defineStore("communication", {
  state: () => {
    return {
      communications: [] as Array<CommunicationViewModel>,
      loading: "loading" as "loading" | "fetched" | "failed",
    };
  },
  actions: {
    fetchCommunicationsForMember() {
      const memberId = useMemberStore().activeMember;
      this.loading = "loading";
      http
        .get(`/admin/member/${memberId}/communications`)
        .then((result) => {
          this.communications = result.data;
          this.loading = "fetched";
        })
        .catch((err) => {
          this.loading = "failed";
        });
    },
    fetchCommunicationById(id: number) {
      const memberId = useMemberStore().activeMember;
      return http.get(`/admin/member/${memberId}/communication/${id}`);
    },
    async createCommunication(communication: CreateCommunicationViewModel): Promise<AxiosResponse<any, any>> {
      const memberId = useMemberStore().activeMember;
      const result = await http.post(`/admin/member/${memberId}/communication`, {
        preferred: communication.preferred,
        mobile: communication.mobile,
        email: communication.email,
        postalCode: communication.postalCode,
        city: communication.city,
        street: communication.street,
        streetNumber: communication.streetNumber,
        streetNumberAddition: communication.streetNumberAddition,
        typeId: communication.typeId,
        isNewsletterMain: communication.isNewsletterMain,
        isSMSAlarming: communication.isSMSAlarming,
      });
      this.fetchCommunicationsForMember();
      return result;
    },
    async updateCommunication(communication: UpdateCommunicationViewModel): Promise<AxiosResponse<any, any>> {
      const memberId = useMemberStore().activeMember;
      const result = await http.patch(`/admin/member/${memberId}/communication/${communication.id}`, {
        preferred: communication.preferred,
        mobile: communication.mobile,
        email: communication.email,
        postalCode: communication.postalCode,
        city: communication.city,
        street: communication.street,
        streetNumber: communication.streetNumber,
        streetNumberAddition: communication.streetNumberAddition,
        isNewsletterMain: communication.isNewsletterMain,
        isSMSAlarming: communication.isSMSAlarming,
      });
      this.fetchCommunicationsForMember();
      return result;
    },
    async deleteCommunication(communication: number): Promise<AxiosResponse<any, any>> {
      const memberId = useMemberStore().activeMember;
      const result = await http.delete(`/admin/member/${memberId}/communication/${communication}`);
      this.fetchCommunicationsForMember();
      return result;
    },
  },
});
