import { useQuery } from "@pinia/colada";
import { useApi } from "~/composables/useApi";
import type { AxiosResponse } from "axios";
import type {
  MarketingPlatform,
  Post,
  ShortVideo,
  Video,
} from "~/types/Marketing";

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

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["marketingVideos", locale.value]),
    query: async () => {
      try {
        const response = (await get(
          "digital-marketing/videos",
        )) as AxiosResponse;

        return response.data.data.videos as Video[];
      } catch (err: any) {
        console.error(err.message);
      }
    },
  });
}
export function useMarketingShortsVideosQuery() {
  const { get } = useApi();
  const { locale } = useI18n();

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["shortVideos", locale.value]),
    query: async () => {
      try {
        const response = (await get("shorts_videos")) as AxiosResponse;

        return response.data.data as ShortVideo[];
      } catch (err: any) {
        console.error(err.message);
      }
    },
  });
}

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

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["marketingPosts", locale.value]),
    query: async () => {
      try {
        const response = (await get(
          "digital-marketing/posts",
        )) as AxiosResponse;

        return response.data.data.posts as Post[];
      } catch (err: any) {
        console.error(err.message);
      }
    },
  });
}

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

  return useQuery({
    staleTime: 1000 * 60 * 60,
    key: computed(() => ["marketingStatics", locale.value]),
    query: async () => {
      try {
        const response = (await get("digital-marketing/tabs")) as AxiosResponse;

        return response.data.data.tabs as MarketingPlatform[];
      } catch (err: any) {
        console.error(err.message);
      }
    },
  });
}
