import { authFetch } from "./client"; export interface Workspace { id: string; name: string; description?: string; owner?: string; my_role?: 'owner' | 'admin' | 'member' | 'guest'; [key: string]: any; } export interface PaginatedResponse { count: number; next: string | null; previous: string | null; results: T[]; } export interface WorkspaceMembership { id: string; workspace: string; user: { id: string; email: string; first_name?: string; last_name?: string; [key: string]: any; }; role: 'owner' | 'admin' | 'member' | 'guest'; is_active: boolean; joined_at?: string; [key: string]: any; } export const fetchWorkspaces = async (params?: Record): Promise> => { const query = params ? new URLSearchParams(params).toString() : ''; const url = `/api/workspaces/${query ? `?${query}` : ''}`; const response = await authFetch(url); if (!response.ok) { throw new Error("Failed to fetch workspaces"); } const data = await response.json(); if (Array.isArray(data)) { return { count: data.length, next: null, previous: null, results: data }; } return { count: data.count || data.results?.length || 0, next: data.next || null, previous: data.previous || null, results: data.results || data }; }; export const getWorkspace = async (id: string): Promise => { const response = await authFetch(`/api/workspaces/${id}/`); if (!response.ok) throw new Error("Failed to fetch workspace details"); return await response.json(); }; export const createWorkspace = async (data: { name: string; description: string; members?: any[] }): Promise => { const response = await authFetch('/api/workspaces/', { method: 'POST', body: JSON.stringify(data), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to create workspace'); } return await response.json(); }; export const updateWorkspace = async (id: string, data: { name?: string; description?: string }): Promise => { const response = await authFetch(`/api/workspaces/${id}/`, { method: 'PATCH', body: JSON.stringify(data), }); if (!response.ok) { const errorData = await response.json(); throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to update workspace'); } return await response.json(); }; export const deleteWorkspace = async (id: string): Promise => { const response = await authFetch(`/api/workspaces/${id}/`, { method: 'DELETE', }); if (!response.ok) { throw new Error('Failed to delete workspace'); } }; export const fetchWorkspaceMemberships = async (params?: Record): Promise> => { const queryParams = new URLSearchParams((params || {})); const response = await authFetch(`/api/workspace-memberships/?${queryParams.toString()}`); if (!response.ok) throw new Error("Failed to fetch workspace memberships"); const data = await response.json(); if (Array.isArray(data)) { return { count: data.length, next: null, previous: null, results: data }; } return { count: data.count || data.results?.length || 0, next: data.next || null, previous: data.previous || null, results: data.results || data }; }; export const addWorkspaceMembership = async (data: { workspace: string; user: string; role: string }) => { const response = await authFetch(`/api/workspace-memberships/`, { method: 'POST', body: JSON.stringify(data), }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to add workspace membership'); } return await response.json(); }; export const removeWorkspaceMembership = async (membershipId: string): Promise => { const response = await authFetch(`/api/workspace-memberships/${membershipId}/`, { method: 'DELETE', }); if (!response.ok) { throw new Error('Failed to remove workspace membership'); } }; export const updateWorkspaceMembership = async (membershipId: string | number, data: { role: string }) => { const response = await authFetch(`/api/workspace-memberships/${membershipId}/`, { method: 'PATCH', body: JSON.stringify(data), }); if (!response.ok) { const errorData = await response.json().catch(() => ({})); throw new Error(errorData.error || JSON.stringify(errorData) || 'Failed to update membership'); } return await response.json(); };