import { axiosInstance } from "./axios";

export interface EcosystemOffering {
  id: string;
  name: string;
  type: string; // "PRODUCT" | "SERVICE"
  tagline: string;
  description: string;
  benefits: string[];
  websiteUrl: string;
  ctaText: string;
  logoIcon: string;
  logoUrl?: string | null;
  mediaUrl?: string | null;
  mediaType?: string | null;
  orderIndex: number;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

export const getEcosystemOfferings = async (): Promise<EcosystemOffering[]> => {
  const response = await axiosInstance.get<EcosystemOffering[]>("/ecosystem/offerings");
  return response.data;
};

export const createEcosystemOffering = async (
  payload: Omit<Partial<EcosystemOffering>, 'id' | 'createdAt' | 'updatedAt'>
): Promise<EcosystemOffering> => {
  const response = await axiosInstance.post<EcosystemOffering>("/ecosystem/offerings", payload);
  return response.data;
};

export const updateEcosystemOffering = async (
  id: string,
  payload: Partial<EcosystemOffering>
): Promise<EcosystemOffering> => {
  const response = await axiosInstance.put<EcosystemOffering>(`/ecosystem/offerings/${id}`, payload);
  return response.data;
};

export const deleteEcosystemOffering = async (id: string): Promise<void> => {
  await axiosInstance.delete(`/ecosystem/offerings/${id}`);
};

export const uploadEcosystemFile = async (file: File): Promise<{ url: string }> => {
  const formData = new FormData();
  formData.append("file", file);
  const response = await axiosInstance.post<{ url: string }>("/ecosystem/upload", formData, {
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
  return response.data;
};

// ── Content Showcase (YouTube Videos) ──

export interface ContentShowcase {
  id: string;
  title: string;
  description?: string | null;
  youtubeUrl: string;
  thumbnailUrl?: string | null;
  redirectUrl?: string | null;
  redirectLabel?: string | null;
  orderIndex: number;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

export const getShowcaseContent = async (): Promise<ContentShowcase[]> => {
  const response = await axiosInstance.get<ContentShowcase[]>("/ecosystem/showcase");
  return response.data;
};

export const createShowcaseContent = async (
  payload: Omit<Partial<ContentShowcase>, 'id' | 'createdAt' | 'updatedAt'>
): Promise<ContentShowcase> => {
  const response = await axiosInstance.post<ContentShowcase>("/ecosystem/showcase", payload);
  return response.data;
};

export const updateShowcaseContent = async (
  id: string,
  payload: Partial<ContentShowcase>
): Promise<ContentShowcase> => {
  const response = await axiosInstance.put<ContentShowcase>(`/ecosystem/showcase/${id}`, payload);
  return response.data;
};

export const deleteShowcaseContent = async (id: string): Promise<void> => {
  await axiosInstance.delete(`/ecosystem/showcase/${id}`);
};

export const getYoutubeMeta = async (url: string): Promise<{ title: string; description: string; thumbnailUrl: string }> => {
  const response = await axiosInstance.get<{ title: string; description: string; thumbnailUrl: string }>("/ecosystem/video-meta", {
    params: { url }
  });
  return response.data;
};

