/**
 * API client for the named-dashboards backend (Slice 3).
 */
import { del, get, post, put } from "./shared";

export interface NamedDashboard {
	id: string;
	label: string;
	slug: string;
	sort_order: number;
	payload: Record<string, unknown>;
	created_at: number;
	updated_at: number;
}

export interface NamedDashboardInput {
	id?: string | null;
	label: string;
	slug?: string;
	sort_order: number;
	payload: Record<string, unknown>;
}

export async function fetchDashboards(): Promise<NamedDashboard[]> {
	return get<NamedDashboard[]>("/api/dashboards");
}

export async function fetchDashboardBySlug(
	slug: string,
): Promise<NamedDashboard> {
	return get<NamedDashboard>(
		`/api/dashboards/slug/${encodeURIComponent(slug)}`,
	);
}

export async function createDashboard(
	input: NamedDashboardInput,
): Promise<NamedDashboard> {
	return post<NamedDashboard>("/api/dashboards", input);
}

export async function updateDashboard(
	input: NamedDashboardInput,
): Promise<NamedDashboard> {
	return put<NamedDashboard>(`/api/dashboards`, input);
}

export async function deleteDashboard(id: string): Promise<{ status: string }> {
	return del<{ status: string }>(`/api/dashboards/${id}`);
}
