export default function useSubscription() {
  function add(subscriptionId: string, user: string) {
    return useNuxtApp().$api("/api/subscriptions", {
      method: "POST",
      body: {
        subscriptionId,
        user,
      },
    });
  }

  function getById(id: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/subscriptions/id/${id}`
    );
  }

  function getByAffiliateId(id: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/subscriptions/affiliate/${id}`
    );
  }

  function getByOrgId(id: string) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/subscriptions/org/${id}`
    );
  }

  function getSubscriptions() {
    return useNuxtApp().$api("/api/subscriptions");
  }

  function getSubscriptionStatusById(id: string) {
    return useNuxtApp().$api<Record<string, string>>(
      `/api/subscriptions/status/${id}`
    );
  }

  function cancelSubscription(id: string) {
    return useNuxtApp().$api(`/api/subscriptions/cancel/${id}`, {
      method: "DELETE",
    });
  }

  const affiliateSubscription = useState(
    "affiliateSubscription",
    () => "inactive"
  );

  async function affSubscriptionStatus() {
    const { currentUser } = useLocalAuth();

    if (currentUser.value && currentUser.value._id) {
      try {
        const result = await getByAffiliateId(currentUser.value._id);
        affiliateSubscription.value = result.status as string;
      } catch (error) {
        console.error("failed to get the subscription", error);
      }
    }
  }

  const orgSubscription = useState("orgSubscription", () => "inactive");

  async function orgSubscriptionStatus() {
    const { currentOrg } = useOrg();

    if (currentOrg.value) {
      try {
        const result = await getByOrgId(currentOrg.value);
        orgSubscription.value = result.status as string;
      } catch (error) {
        console.error("failed to get the subscription", error);
      }
    }
  }

  type TSub = {
    amount: number;
    payment_method_type: string;
    payment_method_card_number?: string;
    payment_method_expiry_month?: string;
    payment_method_expiry_year?: string;
    payment_method_cvv?: string;
    payment_method_cardholder_name?: string;
    currency?: string;
    seats?: number;
    organization?: TOrg;
    billingAddress: TAddress;
    promoCode?: string;
    user?: string;
  };

  function initOrgSubscription(value: TSub) {
    return useNuxtApp().$api<Record<string, any>>(
      "/api/subscriptions/organization",
      {
        method: "POST",
        body: value,
      }
    );
  }

  function initAffiliateSubscription(value: TSub) {
    return useNuxtApp().$api<Record<string, any>>(
      "/api/subscriptions/affiliate",
      {
        method: "POST",
        body: value,
      }
    );
  }

  function updateSeatsById({
    subscriptionId = "",
    seats = 0,
    amount = 0,
    promoCode = "",
  } = {}) {
    return useNuxtApp().$api<Record<string, any>>(
      `/api/subscriptions/seats/${subscriptionId}`,
      {
        method: "PUT",
        body: {
          seats,
          amount,
          promoCode,
        },
      }
    );
  }

  return {
    add,
    getById,
    getByOrgId,
    getSubscriptions,
    affSubscriptionStatus,
    affiliateSubscription,
    orgSubscriptionStatus,
    orgSubscription,
    getSubscriptionStatusById,
    cancelSubscription,
    initAffiliateSubscription,
    initOrgSubscription,
    getByAffiliateId,
    updateSeatsById,
  };
}
