import { HandCoins } from "lucide-react";
import Link from "next/link";
import { redirect } from "next/navigation";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/auth";
import {
	Table,
	TableBody,
	TableCaption,
	TableCell,
	TableFooter,
	TableHead,
	TableHeader,
	TableRow,
} from "@/components/ui/table";
import { cn } from "@/lib/utils";
import { getDevices } from "@/queries/devices";
import { tryCatch } from "@/utils/tryCatch";
import BlockDeviceDialog from "../block-device-dialog";
import ClientErrorMessage from "../client-error-message";
import Pagination from "../pagination";

export async function AdminDevicesTable({
	searchParams,
}: {
	searchParams: Promise<{
		[key: string]: unknown;
	}>;
}) {
	const resolvedParams = await searchParams;
	const session = await getServerSession(authOptions);
	const isAdmin = session?.user?.is_admin;

	const page = Number.parseInt(resolvedParams.page as string) || 1;
	const limit = 10;
	const offset = (page - 1) * limit;

	// Build params object for getDevices
	const apiParams: Record<string, string | number | undefined> = {};
	for (const [key, value] of Object.entries(resolvedParams)) {
		if (value !== undefined && value !== "") {
			apiParams[key] = typeof value === "number" ? value : String(value);
		}
	}
	apiParams.limit = limit;
	apiParams.offset = offset;

	const [error, devices] = await tryCatch(getDevices(apiParams, true));
	if (error) {
		if (error.message === "UNAUTHORIZED") {
			redirect("/auth/signin");
		} else {
			return <ClientErrorMessage message={error.message} />;
		}
	}
	const { meta, data } = devices;
	return (
		<div>
			{data?.length === 0 ? (
				<div className="h-[calc(100svh-400px)] text-muted-foreground flex flex-col items-center justify-center my-4">
					<h3>No devices.</h3>
				</div>
			) : (
				<>
					<div>
						<Table className="overflow-scroll">
							<TableCaption>Table of all devices.</TableCaption>
							<TableHeader>
								<TableRow>
									<TableHead>Device Name</TableHead>
									<TableHead>User</TableHead>
									<TableHead>MAC Address</TableHead>
									<TableHead>Vendor</TableHead>
									<TableHead>#</TableHead>
								</TableRow>
							</TableHeader>
							<TableBody className="overflow-scroll">
								{data?.map((device) => (
									<TableRow key={device.id}>
										<TableCell>
											<div className="flex flex-col items-start">
												<Link
													className={cn(
														"hover:underline font-semibold",
														device.is_active ? "text-green-600" : "",
													)}
													href={`/devices/${device.id}`}
												>
													{device.name}
												</Link>
												{device.is_active ? (
													<div className="text-muted-foreground">
														Active until{" "}
														<span className="font-semibold">
															{new Date(
																device.expiry_date || "",
															).toLocaleDateString("en-US", {
																month: "short",
																day: "2-digit",
																year: "numeric",
															})}
														</span>
													</div>
												) : (
													<p className="text-muted-foreground">
														Device Inactive
													</p>
												)}
												{device.has_a_pending_payment && (
													<Link href={`/payments/${device.pending_payment_id}`}>
														<span className="bg-muted rounded px-2 p-1 mt-2 flex hover:underline items-center justify-center gap-2 text-muted-foreground">
															Payment Pending{" "}
															<HandCoins className="animate-pulse" size={14} />
														</span>
													</Link>
												)}

												{device.blocked_by === "ADMIN" && device.blocked && (
													<div className="p-2 rounded border my-2 bg-white dark:bg-neutral-800 shadow">
														<span className="font-semibold">Comment</span>
														<p className="text-neutral-400">
															{device?.reason_for_blocking}
														</p>
													</div>
												)}
											</div>
										</TableCell>
										<TableCell className="font-medium">
											<div className="flex flex-col items-start">
												{device?.user?.name}
												<span className="text-muted-foreground">
													{device?.user?.id_card}
												</span>
											</div>
										</TableCell>
										<TableCell className="font-medium">{device.mac}</TableCell>
										<TableCell className="font-medium">
											{device?.vendor}
										</TableCell>
										<TableCell>
											{!device.has_a_pending_payment && (
												<BlockDeviceDialog
													admin={isAdmin}
													type={device.blocked ? "unblock" : "block"}
													device={device}
												/>
											)}
										</TableCell>
									</TableRow>
								))}
							</TableBody>
							<TableFooter>
								<TableRow>
									<TableCell colSpan={5} className="text-muted-foreground">
										{meta?.total === 1 ? (
											<p className="text-center">Total {meta?.total} device.</p>
										) : (
											<p className="text-center">
												Total {meta?.total} devices.
											</p>
										)}
									</TableCell>
								</TableRow>
							</TableFooter>
						</Table>
					</div>

					<Pagination
						totalPages={meta?.last_page}
						currentPage={meta?.current_page}
					/>
				</>
			)}
		</div>
	);
}
