// Saved "smart views": per-user named snapshots of FeedFilters (see SavedViewsWidget).
// Only touches core's req; the FeedFilters shape is owned by feed.ts.
import { req } from "./core";
import type { FeedFilters } from "./feed";

// A saved "smart view": a per-user named snapshot of FeedFilters (see SavedViewsWidget).
export interface SavedView {
  id: number;
  name: string;
  filters: FeedFilters;
  position: number;
  is_default: boolean;
}

export const savedViewsApi = {
  savedViews: (): Promise<SavedView[]> => req("/api/saved-views"),
  createView: (name: string, filters: FeedFilters): Promise<SavedView> =>
    req("/api/saved-views", { method: "POST", body: JSON.stringify({ name, filters }) }),
  updateView: (
    id: number,
    patch: Partial<{ name: string; filters: FeedFilters; is_default: boolean }>,
  ): Promise<SavedView> =>
    req(`/api/saved-views/${id}`, { method: "PATCH", body: JSON.stringify(patch) }),
  deleteView: (id: number): Promise<{ deleted: number }> =>
    req(`/api/saved-views/${id}`, { method: "DELETE" }),
  reorderViews: (ids: number[]): Promise<{ ok: boolean }> =>
    req("/api/saved-views/reorder", { method: "POST", body: JSON.stringify({ ids }) }),
};
