import { API_BASE_URL } from "@/config/const";
import { AboutResponse, Shop } from "../(Main)/about/types/Types";
import axios from "axios";

export async function getShop(slug: string): Promise<Shop> {
  try {
    const response = await axios(`${API_BASE_URL}/public/shops/${slug}`, {
      // cache: "no-store",

      headers: {
        "Content-Type": "application/json",
        "X-SLUG": slug,
      },
    });

    console.log("response", response);

    if (!response.data) {
      if (response.status === 404) {
        throw new Error("SHOP_NOT_FOUND");
      }
      throw new Error(`Failed to fetch shop: ${response.status}`);
    }

    const data: AboutResponse = await response.data;

    if (!data.success || !data.data) {
      throw new Error("SHOP_NOT_FOUND");
    }

    return data.data;
  } catch (error) {
    console.log("error", error);

    if (error instanceof Error && error.message === "SHOP_NOT_FOUND") {
      throw error;
    }
    throw new Error(`Failed to fetch shop data: ${error}`);
  }
}
