//import { fixTime } from "@/utils/fixTime";

import { useQuery } from "@tanstack/react-query";
import { type ColumnDef } from "@tanstack/react-table";
import axios from "axios";
import { format } from "date-fns-tz";
import { useState } from "react";
import { toast } from "sonner";
import { Button } from "../../../../../../../components/ui/button";
import { getReaders } from "../../../querys/rfid/getReaders";

// This type is used to define the shape of our data.
// You can use a Zod schema here if you want.
export type Readers = {
	rfidReader_id: string;
	reader: string;
	readerIP: string;
	lastHeartBeat: string;
	lastTrigger: string;
	lastTriggerGood: boolean;
	active: boolean;
	lastTagScanned: string;
	goodReads: number;
	badReads: number;
	totalReads: number;
	goodRatio: number;
};

export const readerColumns: ColumnDef<Readers>[] = [
	{
		accessorKey: "reader",
		header: () => <div className="text-left">Name</div>,
	},
	{
		accessorKey: "lastHeartBeat",
		header: "Last HeartBeat",
		cell: ({ row }) => {
			if (row.getValue("lastHeartBeat")) {
				const correctDate: any = row.getValue("lastHeartBeat");
				const strippedDate = correctDate.replace("Z", ""); // Remove Z
				const formattedDate = format(strippedDate, "MM/dd/yyyy HH:mm");
				return <div className="text-left font-medium">{formattedDate}</div>;
			}
		},
	},
	{
		accessorKey: "lastTrigger",
		header: "Last Trigger",
		cell: ({ row }) => {
			if (row.getValue("lastTrigger")) {
				const correctDate: any = row.getValue("lastTrigger");
				const strippedDate = correctDate.replace("Z", ""); // Remove Z
				const formattedDate = format(strippedDate, "MM/dd/yyyy HH:mm");
				return <div className="text-left font-medium">{formattedDate}</div>;
			}
		},
	},
	{
		accessorKey: "lastTriggerGood",
		header: "Last Trigger Status",
	},
	{
		accessorKey: "lastTagScanned",
		header: "Last Scanned Tag",
	},
	{
		accessorKey: "goodReads",
		header: "Total Good Reads",
	},
	{
		accessorKey: "badReads",
		header: "Total Bad Reads",
	},
	{
		accessorKey: "totalReads",
		header: "Total Reads",
		cell: ({ row }) => {
			const total =
				parseInt(row.getValue("goodReads")) +
				parseInt(row.getValue("badReads"));
			return <div className="text-left font-medium">{total}</div>;
		},
	},
	{
		accessorKey: "goodRatio",
		header: "Good Ratio",
		cell: ({ row }) => {
			const goodRatio =
				(parseInt(row.getValue("goodReads")) /
					(parseInt(row.getValue("goodReads")) +
						parseInt(row.getValue("badReads")))) *
				100;
			return (
				<div className="text-left font-medium">
					{isNaN(goodRatio) ? 0 : goodRatio.toFixed(2)}%
				</div>
			);
		},
	},
	{
		accessorKey: "reset",
		header: "Reset Reads",
		cell: ({ row }) => {
			const { refetch } = useQuery(getReaders());
			const [readerReset, setReaderReset] = useState(false);
			// const goodRatio =
			//     (parseInt(row.getValue("goodReads")) /
			//         (parseInt(row.getValue("goodReads")) +
			//             parseInt(row.getValue("badReads")))) *
			//     100;
			const name = row.getValue("reader");
			const resetReads = async () => {
				setReaderReset(true);
				try {
					const res = await axios.post("/api/rfid/resetRatio", {
						reader: name,
					});

					if (res.status === 200) {
						toast.success(res.data.message);
						setReaderReset(false);
					} else {
						toast.error(res.data.message);
						setReaderReset(false);
					}
				} catch (error: any) {
					toast.error(error.data.message);
					setReaderReset(false);
				}
				refetch();
			};
			return (
				<div className="text-left font-medium">
					<Button className="h-4" onClick={resetReads} disabled={readerReset}>
						Reset Reads
					</Button>
				</div>
			);
		},
	},
];
