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

import { useQuery } from "@tanstack/react-query";
import { type ColumnDef } from "@tanstack/react-table";
import axios from "axios";
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 Adjustmnets = {
	ratio_id: string;
	name: string;
	autoLabel: number;
	manualLabel: string;
};

export const labelRatioColumns: ColumnDef<Adjustmnets>[] = [
	// {
	//     accessorKey: "line",
	//     header: () => <div className="text-left">Line</div>,
	// },
	{
		accessorKey: "autoLabel",
		header: "Auto Labels",
	},
	{
		accessorKey: "manualLabel",
		header: "Manual Labels",
	},
	{
		accessorKey: "goodRatio",
		header: "Ratio",
		cell: ({ row }) => {
			const goodRatio =
				(parseInt(row.getValue("autoLabel")) /
					(parseInt(row.getValue("autoLabel")) +
						parseInt(row.getValue("manualLabel")))) *
				100;
			return (
				<div className="text-center font-medium">
					{isNaN(goodRatio) ? 0 : goodRatio.toFixed(2)}%
				</div>
			);
		},
	},
	{
		accessorKey: "reset",
		header: "Reset Reads",
		cell: () => {
			const { refetch } = useQuery(getReaders());
			const [readerReset, setReaderReset] = useState(false);
			const resetReads = async () => {
				setReaderReset(true);
				try {
					const res = await axios.post("/lst/old/api/ocp/resetlabelratio", {
						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
					</Button>
				</div>
			);
		},
	},
];
