import {
  httpDeleteInterceptor,
  httpGetInterceptor,
  httpPostFormDataInterceptor,
  httpPostInterceptor,
  httpPutFormDataInterceptor,
  httpPutInterceptor,
} from "./http-config/http-interceptor-services";

/** Axios wrapper returns `{ error, data }` where `data` is full API body `{ success, messages, data }`. */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function apiPayload<T>(res: any): T | null {
  if (!res || typeof res !== "object") return null;
  const r = res as { error?: boolean; data?: { data?: T } | null };
  if (r.error || r.data == null) return null;
  return r.data.data ?? null;
}

export function apiRows<T>(res: any): T[] {
  const rows = apiPayload<T[]>(res);
  return Array.isArray(rows) ? rows : [];
}

/** Normalizes list endpoints that return either a single object or an array. */
export function apiDataList<T>(res: unknown): T[] {
  const raw = apiPayload<T[] | T>(res);
  if (Array.isArray(raw)) return raw;
  if (raw != null && typeof raw === "object") return [raw as T];
  return [];
}

export async function getHeroContent() {
  return await httpGetInterceptor("/hero-contents");
}

export async function saveHeroContent(body: {
  primary_title: string;
  secondary_title?: string;
  description?: string;
  primary_cta?: string;
  secondary_cta_text?: string;
}) {
  return await httpPostInterceptor("/hero-contents", body);
}

export async function updateHeroContent(
  id: string,
  body: {
    primary_title: string;
    secondary_title?: string;
    description?: string;
    primary_cta?: string;
    secondary_cta_text?: string;
  },
) {
  return await httpPutInterceptor(`/hero-contents/${id}`, body);
}

export async function getHeroImages(heroId: string) {
  return await httpGetInterceptor(`/hero-content-images/${heroId}`);
}

export async function saveHeroImage(body: {
  hero_content_id: string;
  image_path?: string;
  image_url?: string;
}) {
  return await httpPostInterceptor("/hero-content-images", body);
}

/** Multipart: fields `hero_content_id`, `file` */
export async function saveHeroImageUpload(formData: FormData) {
  return await httpPostFormDataInterceptor("/hero-content-images", formData);
}

export async function updateHeroImage(
  id: string,
  body: { image_path?: string; image_url?: string },
) {
  return await httpPutInterceptor(`/hero-content-images/${id}`, body);
}

/** Multipart: field `file` */
export async function updateHeroImageUpload(id: string, formData: FormData) {
  return await httpPutFormDataInterceptor(`/hero-content-images/${id}`, formData);
}

export async function getAboutContentsList() {
  return await httpGetInterceptor("/about-us-contents");
}

export async function saveAboutContent(body: Record<string, string>) {
  return await httpPostInterceptor("/about-us-contents", body);
}

export async function updateAboutContent(
  id: number,
  body: Record<string, string>,
) {
  return await httpPutInterceptor(`/about-us-contents/${id}`, body);
}

export async function saveAboutUsMediaUrl(body: {
  about_us_content_id: number;
  media_url: string;
  media_type?: string;
}) {
  return await httpPostInterceptor("/about-us-content-images/url", body);
}

/** Multipart: fields `about_us_content_id`, `file` (image or mp4/webm) */
export async function saveAboutUsMediaUpload(formData: FormData) {
  return await httpPostFormDataInterceptor("/about-us-content-images", formData);
}

export async function deleteAboutUsContentImage(id: number) {
  return await httpDeleteInterceptor(`/about-us-content-images/${id}`);
}

export async function getOurProductContent() {
  return await httpGetInterceptor("/our-product-contents");
}

export async function saveOurProductContent(body: {
  primary_title: string;
  secondary_title?: string;
  description?: string;
  link_url?: string;
}) {
  return await httpPostInterceptor("/our-product-contents", body);
}

export async function updateOurProductContent(
  id: string,
  body: {
    primary_title: string;
    secondary_title?: string;
    description?: string;
    link_url?: string;
  },
) {
  return await httpPutInterceptor(`/our-product-contents/${id}`, body);
}

export async function deleteOurProductContent(id: string) {
  return await httpDeleteInterceptor(`/our-product-contents/${id}`);
}

export async function getOurProductImages(productContentId: string) {
  return await httpGetInterceptor(
    `/our-product-content-images/${productContentId}`,
  );
}

export async function saveOurProductImage(body: {
  our_product_content_id: string;
  image_path?: string;
  image_url?: string;
}) {
  return await httpPostInterceptor("/our-product-content-images", body);
}

/** Multipart: fields `our_product_content_id`, `file` */
export async function saveOurProductImageUpload(formData: FormData) {
  return await httpPostFormDataInterceptor("/our-product-content-images", formData);
}

export async function updateOurProductImage(
  id: string,
  body: { image_path?: string; image_url?: string },
) {
  return await httpPutInterceptor(`/our-product-content-images/${id}`, body);
}

/** Multipart: field `file` */
export async function updateOurProductImageUpload(id: string, formData: FormData) {
  return await httpPutFormDataInterceptor(`/our-product-content-images/${id}`, formData);
}

export async function getOurServiceContent() {
  return await httpGetInterceptor("/our-service-contents");
}

export async function saveOurServiceContent(body: {
  primary_title: string;
  secondary_title?: string;
  description?: string;
  link_url?: string;
}) {
  return await httpPostInterceptor("/our-service-contents", body);
}

export async function updateOurServiceContent(
  id: number,
  body: {
    primary_title: string;
    secondary_title?: string;
    description?: string;
    link_url?: string;
  },
) {
  return await httpPutInterceptor(`/our-service-contents/${id}`, body);
}

export async function deleteOurServiceContent(id: number) {
  return await httpDeleteInterceptor(`/our-service-contents/${id}`);
}

export async function getOurServiceImages(serviceContentId: number) {
  return await httpGetInterceptor(
    `/our-service-content-images/${serviceContentId}`,
  );
}

export async function saveOurServiceImage(body: {
  our_service_content_id: number;
  image_path?: string;
  image_url?: string;
}) {
  return await httpPostInterceptor("/our-service-content-images", body);
}

/** Multipart: fields `our_service_content_id`, `file` */
export async function saveOurServiceImageUpload(formData: FormData) {
  return await httpPostFormDataInterceptor("/our-service-content-images", formData);
}

export async function updateOurServiceImage(
  id: string,
  body: { image_path?: string; image_url?: string },
) {
  return await httpPutInterceptor(`/our-service-content-images/${id}`, body);
}

/** Multipart: field `file` */
export async function updateOurServiceImageUpload(id: string, formData: FormData) {
  return await httpPutFormDataInterceptor(`/our-service-content-images/${id}`, formData);
}

export async function getPartnerContents() {
  return await httpGetInterceptor("/partner-contents");
}

export async function savePartnerContent(body: {
  primary_title: string;
  image_path?: string;
  image_url?: string;
}) {
  return await httpPostInterceptor("/partner-contents", body);
}

export async function updatePartnerContent(
  id: string,
  body: {
    primary_title: string;
    image_path?: string;
    image_url?: string;
  },
) {
  return await httpPutInterceptor(`/partner-contents/${id}`, body);
}

/** Multipart: field `file` — updates logo in MinIO */
export async function uploadPartnerLogo(id: string, formData: FormData) {
  return await httpPostFormDataInterceptor(`/partner-contents/${id}/logo`, formData);
}

export async function deletePartnerContent(id: string) {
  return await httpDeleteInterceptor(`/partner-contents/${id}`);
}

export async function getPopupNewsList(page = 1, limit = 10) {
  return await httpGetInterceptor(
    `/popup-news-contents?page=${page}&limit=${limit}`,
  );
}

export async function getPopupNewsById(id: number) {
  return await httpGetInterceptor(`/popup-news-contents/${id}`);
}

export async function savePopupNews(body: {
  primary_title: string;
  secondary_title?: string;
  description?: string;
  primary_cta?: string;
  secondary_cta_text?: string;
}) {
  return await httpPostInterceptor("/popup-news-contents", body);
}

export async function updatePopupNews(
  id: number,
  body: {
    id: number;
    primary_title: string;
    secondary_title?: string;
    description?: string;
    primary_cta?: string;
    secondary_cta_text?: string;
  },
) {
  return await httpPutInterceptor(`/popup-news-contents/${id}`, body);
}

export async function savePopupNewsImage(body: {
  popup_news_content_id: number;
  media_path?: string;
  media_url?: string;
}) {
  return await httpPostInterceptor("/popup-news-content-images", body);
}

/** Multipart: fields `popup_news_content_id`, `file`; optional `is_thumbnail` */
export async function savePopupNewsImageUpload(formData: FormData) {
  return await httpPostFormDataInterceptor("/popup-news-content-images", formData);
}

export async function deletePopupNews(id: number) {
  return await httpDeleteInterceptor(`/popup-news-contents/${id}`);
}
