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

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

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => [`featuredWebProjects`, locale.value]),
    query: async () => {
      try {
        const response = (await get(`web_projects`)) as AxiosResponse;
        return response.data.data;
      } catch (error: any) {
        console.error(error.message);
      }
    },
  });
}

export function useFeaturedMobileProjects() {
  const { get } = useApi();
  const { locale } = useI18n();
  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => [`featuredMobileProjects`, locale.value]),
    query: async () => {
      try {
        const response = (await get(`app_projects`)) as AxiosResponse;
        return response.data.data;
      } catch (error: any) {
        console.error(error.message);
      }
    },
  });
}

export function useFeaturedProjects() {
  const { get } = useApi();
  const { locale } = useI18n();
  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["featuredPorjects", locale.value]),
    query: async () => {
      try {
        const response = (await get("featured_projects")) as AxiosResponse;
        return response.data.data as FeaturedProject;
      } catch (error: any) {
        console.error(error.message);
      }
    },
  });
}
export function useSpecificFeaturedProjects(id: string) {
  const { get } = useApi();
  const { locale } = useI18n();
  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["featuredPorjects", id, locale.value]),
    query: async () => {
      try {
        const response = (await get(
          `featured_projects?service_id=${id}`,
        )) as AxiosResponse;
        return response.data.data as FeaturedProject;
      } catch (error: any) {
        console.error(error.message);
      }
    },
  });
}
