import type { LoaderFunctionArgs } from "@remix-run/node";
import { json } from "@remix-run/node";
import { requireUser } from "~/lib/auth.server";
import { searchCustomers } from "~/lib/customer-management.server";

export async function loader({ request }: LoaderFunctionArgs) {
  await requireUser(request);
  
  const url = new URL(request.url);
  const query = url.searchParams.get("q") || "";
  const limit = parseInt(url.searchParams.get("limit") || "10");
  
  if (!query || query.trim().length < 2) {
    return json({ customers: [] });
  }
  
  const customers = await searchCustomers(query, limit);
  
  return json({ customers });
}