import { del, get, post, put } from "./shared";
import type {
	ServiceInstance,
	ServiceInstanceInput,
	ServiceTypeInfo,
} from "../types";

export async function fetchServiceTypes(): Promise<ServiceTypeInfo[]> {
	return get<ServiceTypeInfo[]>("/api/services/types");
}

export async function fetchServiceInstances(
	serviceType?: string,
): Promise<ServiceInstance[]> {
	return get<ServiceInstance[]>(
		"/api/services/instances",
		serviceType ? { service_type: serviceType } : undefined,
	);
}

export async function createServiceInstance(
	input: ServiceInstanceInput,
): Promise<ServiceInstance> {
	return post<ServiceInstance>("/api/services/instances", input);
}

export async function updateServiceInstance(
	input: ServiceInstanceInput,
): Promise<ServiceInstance> {
	if (!input.id) throw new Error("Service ID is required for update");
	return put<ServiceInstance>(`/api/services/instances/${input.id}`, input);
}

export async function deleteServiceInstance(
	serviceId: string,
): Promise<{ status: string }> {
	return del<{ status: string }>(`/api/services/instances/${serviceId}`);
}
