import { getWebhookUrl } from '@/lib/config';

export const generateSocialMediaPosts = async (payload: any): Promise<any> => {
  try {
    const { apiFetch } = await import('@/utils/apiFetch');
    const response = await apiFetch(`${getWebhookUrl()}/socialmedia/post2event`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(payload),
    });
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    return await response.json();
  } catch (error) {
    console.error('Error generating social media posts:', error);
    return null;
  }
};

export const publishSocialMediaPost = async ({ postId, posts, images, scheduledTime }: {
  postId: string;
  posts: any;
  images: any;
  scheduledTime: string;
}): Promise<any> => {
  try {
    const { apiFetch } = await import('@/utils/apiFetch');
    const response = await apiFetch(`${getWebhookUrl()}/socialmedia/publish_post`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ postId, posts, images, scheduledTime }),
    });
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    return await response.json();
  } catch (error) {
    console.error('Error publishing social media post:', error);
    return null;
  }
};
