import { SocialGroup } from '../types'; import { apiClient } from './api/client'; interface ApiGroup { id: string; name: string; description?: string; avatar_url?: string; is_public?: boolean; member_count?: number; } function mapApiGroupToSocialGroup(g: ApiGroup, userRole: SocialGroup['userRole'] = 'none'): SocialGroup { return { id: g.id, name: g.name, members: g.member_count ?? 0, isPrivate: !(g.is_public ?? true), userRole, description: g.description ?? '', coverUrl: g.avatar_url ?? 'https://picsum.photos/id/10/800/400', }; } export const groupService = { list: async (): Promise<{ groups: SocialGroup[] }> => { const response = await apiClient.get<{ groups: ApiGroup[]; total?: number }>( '/social/groups', { params: { limit: 50, offset: 0 } }, ); const items = response.data?.groups ?? []; return { groups: items.map((g) => mapApiGroupToSocialGroup(g)), }; }, get: async (id: string) => { const response = await apiClient.get(`/social/groups/${id}`); const g = response.data; if (!g) throw new Error('Group not found'); return { group: mapApiGroupToSocialGroup(g, 'member'), membersList: [], events: [], }; }, create: async (data: { name: string; description?: string; isPrivate?: boolean; coverUrl?: string }) => { const response = await apiClient.post('/social/groups', { name: data.name, description: data.description ?? '', is_public: !data.isPrivate, }); const g = response.data; if (!g) throw new Error('Failed to create group'); return mapApiGroupToSocialGroup(g, 'admin'); }, join: async (id: string) => { await apiClient.post(`/social/groups/${id}/join`); return { success: true }; }, leave: async (id: string) => { await apiClient.delete(`/social/groups/${id}/leave`); return { success: true }; }, };