import { UserCard } from "@/components/cards/UserCard";
import { fetchUsersByUsername } from "@/lib/actions";
import { SearchX } from "lucide-react";
import { redirect } from "next/navigation";

export default async function Search({ searchParams }: { searchParams: any }) {
  const username = searchParams.username;

  if (!username) return redirect("/panel/inspect");
  const users = await fetchUsersByUsername(username);

  if (!users.length) return (
    <>
      <h2 className="mt-8 flex justify-center">
        <SearchX className="text-gray-400" />
      </h2>
      <h3 className="text-xs text-center pb-2 text-gray-400">
        No search results
      </h3>
    </>
  );

  return (
    <div className="flex flex-col gap-2">
      {
        users.map((user) => (
          <a key={user._id} href={`/panel/inspect/user/${user._id}`}>
            <UserCard user={user} subtitle={user._id} />
          </a>
        ))
      }
    </div>
  );
}
