import { api } from './api';

export interface SavedMessage {
    id: string;
    content: string;
    role: string;
    createdAt: string;
}

interface ApiResponse<T> {
    success: boolean;
    data: T;
    error?: string;
}

export const getBookmarks = async (): Promise<SavedMessage[]> => {
    try {
        const res = await api.get<ApiResponse<SavedMessage[]>>('/bookmarks');
        return res.data || [];
    } catch (e) {
        console.error('Failed to fetch bookmarks:', e);
        return [];
    }
};

export const createBookmark = async (content: string, role: string = 'model'): Promise<SavedMessage | null> => {
    try {
        const res = await api.post<ApiResponse<SavedMessage>>('/bookmarks', { content, role });
        return res.data || null;
    } catch (e) {
        console.error('Failed to create bookmark:', e);
        return null;
    }
};

export const deleteBookmark = async (id: string): Promise<boolean> => {
    try {
        await api.delete(`/bookmarks/${id}`);
        return true;
    } catch (e) {
        console.error('Failed to delete bookmark:', e);
        return false;
    }
};
