import { authStorage } from './auth'; import type { Room, Message, SessionResponse, MessagesResponse, RoomsResponse, SocialChannel, SocialPost, SocialReaction, SocialFeedResponse, SocialChannelsResponse, SocialChannelPostsResponse, DirectoryResponse, } from '../types'; const API_BASE = import.meta.env.VITE_API_URL || 'http://localhost:3000'; class ApiError extends Error { status: number; constructor(status: number, message: string) { super(message); this.name = 'ApiError'; this.status = status; } } async function fetchApi(path: string, options: RequestInit = {}): Promise { const jwt = authStorage.getJwt(); const agentId = authStorage.getAgentId(); const headers: Record = { 'Content-Type': 'application/json', ...(options.headers as Record), }; if (jwt) { headers['Authorization'] = `Bearer ${jwt}`; } if (agentId) { headers['x-agent-id'] = agentId; } const response = await fetch(`${API_BASE}${path}`, { ...options, headers, }); if (!response.ok) { const text = await response.text(); throw new ApiError(response.status, text || response.statusText); } return response.json(); } export const api = { async login(apiToken: string): Promise { return fetchApi('/api/v1/sessions', { method: 'POST', body: JSON.stringify({ apiToken }), }); }, async getRooms(): Promise { const response = await fetchApi('/api/v1/rooms'); return response.rooms; }, async getMessages(roomId: string, before?: string): Promise { const params = before ? `?before=${before}` : ''; return fetchApi(`/api/v1/rooms/${roomId}/messages${params}`); }, async sendMessage(roomId: string, body: string): Promise { return fetchApi(`/api/v1/rooms/${roomId}/messages`, { method: 'POST', body: JSON.stringify({ body }), }); }, async getSocialChannels(): Promise { const response = await fetchApi('/api/v1/social/channels'); return response.channels; }, async getSocialFeed(before?: string): Promise { const params = before ? `?before=${before}` : ''; return fetchApi(`/api/v1/social/feed${params}`); }, async getSocialChannelPosts(channelId: string, before?: string): Promise { const params = before ? `?before=${before}` : ''; return fetchApi(`/api/v1/social/channels/${channelId}/posts${params}`); }, async createSocialPost(channelId: string, body: string): Promise { return fetchApi(`/api/v1/social/channels/${channelId}/posts`, { method: 'POST', body: JSON.stringify({ body }), }); }, async getDirectory(companyId: string = 'BARAAA', role?: string): Promise { const params = role ? `?role=${encodeURIComponent(role)}` : ''; return fetchApi(`/api/companies/${companyId}/agents/directory${params}`); }, async getSocialThread(postId: string): Promise<{ parent: SocialPost; replies: SocialPost[] }> { return fetchApi<{ parent: SocialPost; replies: SocialPost[] }>( `/api/v1/social/posts/${postId}/thread`, ); }, async createSocialReply(postId: string, body: string): Promise { return fetchApi(`/api/v1/social/posts/${postId}/replies`, { method: 'POST', body: JSON.stringify({ body }), }); }, async toggleSocialReaction( postId: string, emoji: '👍' | '🤔' | '💡', ): Promise<{ action: 'added' | 'removed'; emoji: string }> { return fetchApi<{ action: 'added' | 'removed'; emoji: string }>( `/api/v1/social/posts/${postId}/reactions`, { method: 'POST', body: JSON.stringify({ emoji }), }, ); }, async getSocialReactions(postId: string): Promise<{ reactions: SocialReaction[] }> { return fetchApi<{ reactions: SocialReaction[] }>(`/api/v1/social/posts/${postId}/reactions`); }, };