import type { ColumnDef } from "@tanstack/react-table";
import { Cog } from "lucide-react";
import { Button } from "~/components/ui/button";
import { DataTable } from "~/components/ui/data-table";
import { DataTableColumnHeader } from "~/components/ui/dataTable-header";
import type { SearchFiltro } from "~/components/ui/dataTable-toolbar";
import type { Banlist } from "~/schemas/public/Banlist";

export const BanlistTable = (props: {
	data: Banlist[];
	setOpenEdit: (status: boolean) => void;
	setEditData: (data: Banlist | null) => void;
}) => {
	const { data, setOpenEdit, setEditData } = props;

	const tabledata = data.map((row) => {
		return {
			actions: "Azioni",
			id: row.id,
			type: row.type,
			value: row.value,
		};
	});

	const columns_titles = {
		actions: "Azioni",
		id: "Codice",
		type: "Tipo",
		value: "Valore",
	};
	type Column = (typeof tabledata)[number];

	const columns: ColumnDef<Column>[] = [
		{
			accessorKey: "id",
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.id} />
			),
		},
		{
			accessorKey: "type",
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.type} />
			),
		},
		{
			accessorKey: "value",
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.value} />
			),
		},

		{
			accessorKey: "actions",
			cell: ({ row }) => {
				const data = row.original;
				return (
					<Button
						aria-label="Edit Banlist Entry"
						onClick={() => {
							setEditData(data);
							setOpenEdit(true);
						}}
						variant="outline"
					>
						<div className="flex items-center gap-2">
							<Cog /> Modifica
						</div>
					</Button>
				);
			},
			enableHiding: false,
			enableSorting: false,
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.actions} />
			),
		},
	];

	const searchFiltro: SearchFiltro = {
		columnName: "value",
		placeholder: "Filtra ...",
	};
	return (
		<div className="mx-auto w-full px-2 py-10">
			<DataTable
				columns={columns}
				columns_titles={columns_titles}
				data={tabledata}
				defaultSort={[{ desc: true, id: "id" }]}
				pinnedFiltri={undefined}
				searchColumn={searchFiltro}
			/>
		</div>
	);
};
