/**
 * HTTP-based tRPC link for the standalone web SPA.
 *
 * Replaces the Electron IPC link with standard HTTP transport so the
 * same tRPC router types are used against the FastAPI backend.
 */
import { httpBatchLink } from '@trpc/client';
import type { AnyRouter } from '@trpc/server';

/**
 * Returns an httpBatchLink configured for the web environment.
 * Reads the API URL from `VITE_API_URL` (falls back to `/api/trpc`).
 * Attaches the JWT token from localStorage on every request.
 */
export function createHttpLink<TRouter extends AnyRouter>() {
  return httpBatchLink<TRouter>({
    url: (import.meta as any).env?.VITE_API_URL ?? '/api/trpc',
    headers() {
      const token = localStorage.getItem('adiuvai-token');
      return token ? { Authorization: `Bearer ${token}` } : {};
    },
  });
}
