import { del, get, post, put } from "./shared";
import type {
	BuiltinWidgetKindInfo,
	WidgetDataResponse,
	WidgetInstance,
	WidgetInstanceInput,
} from "../types";

export interface WidgetReference {
	id: string;
	dashboard_scope: string;
	widget_id: string;
	sort_order: number;
	created_at: number;
	widget: WidgetInstance;
}

export interface WidgetReferenceInput {
	dashboard_scope: string;
	widget_id: string;
	sort_order?: number;
}

export async function fetchBuiltinWidgetKinds(): Promise<
	BuiltinWidgetKindInfo[]
> {
	return get<BuiltinWidgetKindInfo[]>("/api/widgets/builtin");
}

export async function fetchWidgetInstances(
	serviceId?: string,
	scope?: "dashboard" | "service",
): Promise<WidgetInstance[]> {
	const params: Record<string, string> = {};
	if (serviceId) params.service_id = serviceId;
	if (scope) params.scope = scope;
	return get<WidgetInstance[]>("/api/widgets/instances", params);
}

export async function createWidgetInstance(
	input: WidgetInstanceInput,
): Promise<WidgetInstance> {
	return post<WidgetInstance>("/api/widgets/instances", input);
}

export async function updateWidgetInstance(
	input: WidgetInstanceInput,
): Promise<WidgetInstance> {
	if (!input.id) throw new Error("Widget ID is required for update");
	return put<WidgetInstance>(`/api/widgets/instances/${input.id}`, input);
}

export async function deleteWidgetInstance(
	widgetId: string,
): Promise<{ status: string }> {
	return del<{ status: string }>(`/api/widgets/instances/${widgetId}`);
}

export async function fetchWidgetData(
	widgetId: string,
): Promise<WidgetDataResponse> {
	return get<WidgetDataResponse>(`/api/widgets/instances/${widgetId}/data`);
}

export async function fetchWidgetReferences(
	dashboardScope: string,
): Promise<WidgetReference[]> {
	return get<WidgetReference[]>("/api/widgets/references", {
		dashboard_scope: dashboardScope,
	});
}

export async function createWidgetReference(
	input: WidgetReferenceInput,
): Promise<WidgetReference> {
	return post<WidgetReference>("/api/widgets/references", input);
}

export async function deleteWidgetReference(
	referenceId: string,
): Promise<{ status: string }> {
	return del<{ status: string }>(`/api/widgets/references/${referenceId}`);
}

export async function detachWidgetReference(
	referenceId: string,
): Promise<WidgetInstance> {
	return post<WidgetInstance>(`/api/widgets/references/${referenceId}/detach`);
}

export async function updateWidgetReference(
	referenceId: string,
	sortOrder: number,
): Promise<WidgetReference> {
	return put<WidgetReference>(
		`/api/widgets/references/${referenceId}?sort_order=${sortOrder}`,
		{},
	);
}
