import axios from "axios";

export const API_BASE_URL =
  process.env.NEXT_PUBLIC_API_BASE_URL ?? "https://admin.fruxinfo.com/api";

// ---------------------------------------------------------------------------
// Form submissions (client components)
// ---------------------------------------------------------------------------

export const userContactUsAPI = (data: unknown) =>
  axios.post(`${API_BASE_URL}/contactUs`, data);

export const userCareerAPI = (data: unknown) =>
  axios.post(`${API_BASE_URL}/career`, data);

export const userInquiryAPI = (data: unknown) =>
  axios.post(`${API_BASE_URL}/inquiry`, data);

export const userCareerQuestionAPI = (careerType: string) =>
  axios.get(`${API_BASE_URL}/careerQuestion`, { params: { careerType } });

// ---------------------------------------------------------------------------
// Blog (server components — always fresh, no static cache)
// ---------------------------------------------------------------------------

export async function getBlogs() {
  const res = await fetch(`${API_BASE_URL}/blog/list`, { cache: "no-store" });
  if (!res.ok) throw new Error("Failed to fetch blog list");
  const json = await res.json();
  const data = json?.data;
  return Array.isArray(data) ? data : data?.blogs ?? [];
}

export async function getBlog(slug: string) {
  const res = await fetch(`${API_BASE_URL}/blog/detail/${slug}`, {
    cache: "no-store",
  });
  if (!res.ok) return null;
  const json = await res.json();
  return json?.data ?? null;
}
