import { type SharePlatform } from './social-utils'; const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000/api/v1'; export interface TrackShareParams { articleId: string; platform: SharePlatform; userAgent?: string; ipAddress?: string; } export const trackShare = async (params: TrackShareParams): Promise => { try { const response = await fetch(`${API_BASE_URL}/analytics/share`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(params), }); if (!response.ok) { console.warn('Failed to track share event:', response.statusText); return false; } return true; } catch (error) { console.error('Error tracking share event:', error); return false; } }; export const getShareStats = async (articleId?: string) => { try { const url = articleId ? `${API_BASE_URL}/analytics/shares?articleId=${articleId}` : `${API_BASE_URL}/analytics/shares`; const response = await fetch(url, { headers: { 'Content-Type': 'application/json', }, credentials: 'include', // Include cookies for admin auth }); if (!response.ok) { throw new Error(`Failed to fetch share stats: ${response.statusText}`); } return await response.json(); } catch (error) { console.error('Error fetching share stats:', error); throw error; } }; export const getTopSharedArticles = async (limit: number = 10) => { try { const response = await fetch(`${API_BASE_URL}/analytics/shares/top?limit=${limit}`, { headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (!response.ok) { throw new Error(`Failed to fetch top shared articles: ${response.statusText}`); } return await response.json(); } catch (error) { console.error('Error fetching top shared articles:', error); throw error; } }; export const getTotalShareStats = async () => { try { const response = await fetch(`${API_BASE_URL}/analytics/shares/total`, { headers: { 'Content-Type': 'application/json', }, credentials: 'include', }); if (!response.ok) { throw new Error(`Failed to fetch total share stats: ${response.statusText}`); } return await response.json(); } catch (error) { console.error('Error fetching total share stats:', error); throw error; } };