import { redirect } from "next/navigation";
import { getServerSession } from "next-auth";
import { Suspense } from "react";
import { authOptions } from "@/app/auth";
import { AdminDevicesTable } from "@/components/admin/admin-devices-table";
import DynamicFilter from "@/components/generic-filter";

export default async function UserDevices({
	searchParams,
}: {
	searchParams: Promise<{
		query: string;
		page: number;
		sortBy: string;
		status: string;
	}>;
}) {
	const query = (await searchParams)?.query || "";
	const session = await getServerSession(authOptions);
	if (!session?.user?.is_admin) redirect("/devices?page=1");
	return (
		<div>
			<div className="flex justify-between items-center border rounded-md border-dashed font-bold title-bg py-4 px-2 mb-4">
				<h3 className="text-sarLinkOrange text-2xl">User Devices</h3>
			</div>

			<div
				id="user-filters"
				className=" pb-4 gap-4 flex sm:flex-row flex-col items-start justify-start"
			>
				<DynamicFilter
					description="Filter devices by name, MAC address, or vendor."
					title="Device Filter"
					inputs={[
						{
							name: "name",
							label: "Device Name",
							type: "string",
							placeholder: "Enter device name",
						},
						{
							name: "mac",
							label: "MAC Address",
							type: "string",
							placeholder: "Enter device MAC address",
						},
						{
							name: "vendor",
							label: "Vendor",
							type: "string",
							placeholder: "Enter device vendor name",
						},
						{
							name: "user",
							label: "Device User",
							type: "string",
							placeholder: "User name or id card",
						},
					]}
				/>
			</div>
			<Suspense key={query} fallback={"loading...."}>
				<AdminDevicesTable searchParams={searchParams} />
			</Suspense>
		</div>
	);
}
