import { useQuery } from "@pinia/colada";
import { useApi } from "~/composables/useApi";
import type { AxiosResponse } from "axios";

export function useCountriesQuery() {
  const { get } = useApi();
  const { locale } = useI18n();

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["countries", locale.value]),
    query: async () => {
      try {
        const response = (await get("countries")) as AxiosResponse;
        return response.data.data;
      } catch (error) {
        console.error(error instanceof Error ? error.message : "Unknown error");
        return [];
      }
    },
  });
}

export function useServicesQuery() {
  const { get } = useApi();
  const { locale } = useI18n();

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["services", locale.value]),
    query: async () => {
      try {
        const response = (await get("services")) as AxiosResponse;
        return response.data.data;
      } catch (error) {
        console.error(error instanceof Error ? error.message : "Unknown error");
        return [];
      }
    },
  });
}

export function useBranchesQuery() {
  const { get } = useApi();
  const { locale } = useI18n();

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["branches", locale.value]),
    query: async () => {
      try {
        const response = (await get("branches")) as AxiosResponse;
        return response.data.data;
      } catch (error) {
        console.error(error instanceof Error ? error.message : "Unknown error");
        return [];
      }
    },
  });
}

export function useSocialIconsQuery() {
  const { get } = useApi();

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: ["socialIcons"],
    query: async () => {
      try {
        const response = (await get("general")) as AxiosResponse;

        // Ensure data exists and has the expected structure
        const data = response.data.data ?? {
          snapchat_link: null,
          facebook_link: null,
          instagram_link: null,
          tiktok_link: null,
          X_link: null,
          linkedIn_link: null,
          youtube_link: null,
        };

        // Filter out social icons with null/empty links to avoid rendering empty links
        const socialIcons = [
          {
            name: "snapchat",
            link: data.snapchat_link,
          },
          {
            name: "facebook",
            link: data.facebook_link,
          },
          {
            name: "instagram",
            link: data.instagram_link,
          },
          {
            name: "tiktok",
            link: data.tiktok_link,
          },
          {
            name: "X",
            link: data.X_link,
          },
          {
            name: "linkedIn",
            link: data.linkedIn_link,
          },
          {
            name: "youtube",
            link: data.youtube_link,
          },
        ].filter(
          (icon) =>
            icon.link !== null && icon.link !== undefined && icon.link !== "",
        );

        return socialIcons;
      } catch (error) {
        console.error(error instanceof Error ? error.message : "Unknown error");
        return [];
      }
    },
  });
}
export function useSettingsQuery() {
  const { get } = useApi();

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: ["settings"],
    query: async () => {
      try {
        const response = (await get("general")) as AxiosResponse;

        return response.data.data;
      } catch (error) {
        console.error(error instanceof Error ? error.message : "Unknown error");
        return [];
      }
    },
  });
}
