import axios from "axios";
import { API_BASE_URL } from "@/config/const";
import type { AboutResponse } from "../(Main)/about/types/Types";
import type {
  CategoriesResponse,
  FoodsResponse,
  SlidersResponse,
} from "../(Main)/types/Types";
import { getRestaurant, RESTAURANT_FETCH_TIMEOUT_MS } from "./getRestaurant";

function createServerAxios(slug: string) {
  return axios.create({
    baseURL: API_BASE_URL,
    headers: {
      "Content-Type": "application/json",
      "X-SLUG": slug,
    },
    timeout: RESTAURANT_FETCH_TIMEOUT_MS,
  });
}

export async function fetchAboutResponse(slug: string): Promise<AboutResponse> {
  const restaurant = await getRestaurant(slug);
  return { success: true, data: restaurant };
}

export async function fetchFoodsResponse(slug: string): Promise<FoodsResponse> {
  const client = createServerAxios(slug);
  const { data } = await client.get<FoodsResponse>(
    `/public/foods/restaurant/${slug}`,
  );
  return data;
}

export async function fetchCategoriesResponse(
  slug: string,
): Promise<CategoriesResponse> {
  const client = createServerAxios(slug);
  const { data } = await client.get<CategoriesResponse>(
    `/public/categories/restaurant/${slug}`,
  );
  return data;
}

export async function fetchSlidersResponse(
  slug: string,
): Promise<SlidersResponse> {
  try {
    const client = createServerAxios(slug);
    const { data } = await client.get<SlidersResponse>(
      `/public/sliders/restaurant/${slug}`,
    );
    return data;
  } catch {
    return { success: true, data: [] };
  }
}
