import api from "@/lib/api/axios";
import type {
  AdminCustomersListParams,
  AdminCustomersListResponse,
} from "@/types/customers";

/**
 * Service Customers API (`users/admin/customers`).
 *
 * Two guarded surfaces on one controller prefix: `/users/admin/*` is
 * and only opts in/out of them. The provider is resolved from the JWT, never
 * from a path param.
 *
 * The backend validates with `forbidNonWhitelisted: true`, so undefined params
 * are stripped before sending — an explicit `undefined` would serialize away
 * anyway, but an empty `search=""` would not, and the backend would treat it as
 * a real (always-matching) filter. Boolean filters are declared
 * `@IsBooleanString` backend-side, so they go over the wire as "true"/"false".
 */

// ─── Admin ───────────────────────────────────────────────────────────────────

export async function listAdminCustomersApi(
  params: AdminCustomersListParams,
): Promise<AdminCustomersListResponse> {
  const { data } = await api.get<AdminCustomersListResponse>(
    "/users/admin/customers",
    {
      params: {
        ...(params.search?.trim() ? { search: params.search.trim() } : {}),
        ...(params.isActive !== undefined
          ? { isActive: String(params.isActive) }
          : {}),
        ...(params.page ? { page: params.page } : {}),
        ...(params.limit ? { limit: params.limit } : {}),
      },
    },
  );
  return data;
}
