import type { ColumnDef } from "@tanstack/react-table";
import { CircleSlash, Cog } from "lucide-react";
import { DataTable } from "~/components/custom_ui/data-table";
import { DataTableColumnHeader } from "~/components/custom_ui/dataTable-header";
import type {
	PinnedFiltro,
	SearchFiltro,
} from "~/components/custom_ui/dataTable-toolbar";
import { Badge } from "~/components/ui/badge";
import { Button } from "~/components/ui/button";
import { cn, formatCurrency } from "~/lib/utils";
import type { Prezziario } from "~/schemas/public/Prezziario";

export const PrezzarioTable = ({
	data,
	setOpenEdit,
	setEditData,
}: {
	data: Prezziario[];
	setOpenEdit: (status: boolean) => void;
	setEditData: (data: Prezziario | null) => void;
}) => {
	const tabledata = data.map((row) => {
		return {
			actions: "Azioni",
			categoria: [
				row.isAcconto && "acconto",
				row.isSaldo && "saldo",
				row.isConsulenza && "consulenza",
			].filter(Boolean),
			desc_en: row.desc_en,
			desc_it: row.desc_it,
			id: row.idprezziario,
			isAcconto: row.isAcconto,
			isActive: row.isActive,
			isConsulenza: row.isConsulenza,
			isSaldo: row.isSaldo,
			isStabile: row.isStabile,
			isTransitorio: row.isTransitorio,
			nome_en: row.nome_en,
			nome_it: row.nome_it,
			prezzo_cent: row.prezzo_cent,
			sconto: row.sconto,
			testo_condizioni: row.testo_condizioni,
		};
	});

	const cat_options = [
		{ label: "acconto", value: "acconto" },
		{ label: "saldo", value: "saldo" },
		{ label: "consulenza", value: "consulenza" },
	];

	const columns_titles = {
		actions: "Azioni",
		categoria: "Categoria",
		id: "Codice",
		nome: "Nome",
		prezzo: "Prezzo",
		sconto: "Sconto",
		tipologia: "Tipo",
	};
	type Column = (typeof tabledata)[number];
	const includesAll = (arr: string[], values: string[]) =>
		values.every((v) => arr.includes(v));
	const columns: ColumnDef<Column>[] = [
		{
			accessorKey: "id",
			enableHiding: false,
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.id} />
			),
		},
		{
			accessorKey: "nome",
			cell: ({ row }) => {
				return (
					<div className={cn(!row.original.isActive && "line-through")}>
						{row.original.nome_it}
					</div>
				);
			},
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.nome} />
			),
		},
		{
			accessorKey: "categoria",
			filterFn: (row, id, value) => {
				return includesAll(row.getValue(id), value as string[]);
			},
			header: ({ column }) => (
				<DataTableColumnHeader
					column={column}
					title={columns_titles.categoria}
				/>
			),
		},

		{
			accessorKey: "tipologia",
			cell: ({ row }) => {
				return (
					<div>
						{row.original.isTransitorio
							? row.original.isStabile
								? "Transitorio/Stabile"
								: "Transitorio"
							: row.original.isStabile
								? "Stabile"
								: ""}
					</div>
				);
			},
			filterFn: (row, id, value) => {
				return includesAll(row.getValue(id), value as string[]);
			},
			header: ({ column }) => (
				<DataTableColumnHeader
					column={column}
					title={columns_titles.tipologia}
				/>
			),
		},
		{
			accessorKey: "prezzo",
			cell: ({ row }) => {
				return <div>{formatCurrency(row.original.prezzo_cent / 100)}</div>;
			},
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.prezzo} />
			),
		},
		{
			accessorKey: "sconto",
			cell: ({ row }) => {
				if (row.original.sconto === 0) {
					return (
						<Badge variant="secondary">
							<CircleSlash className="size-4" />
						</Badge>
					);
				}
				return <Badge>{row.original.sconto}%</Badge>;
			},
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.sconto} />
			),
		},
		{
			accessorKey: "actions",
			cell: ({ row }) => {
				const rowData = row.original;
				return (
					<Button
						aria-label="Modifica Prezziario"
						onClick={() => {
							setEditData({
								desc_en: rowData.desc_en,
								desc_it: rowData.desc_it,
								idprezziario: rowData.id,
								isAcconto: rowData.isAcconto,
								isActive: rowData.isActive,
								isConsulenza: rowData.isConsulenza,
								isSaldo: rowData.isSaldo,
								isStabile: rowData.isStabile,
								isTransitorio: rowData.isTransitorio,
								nome_en: rowData.nome_en,
								nome_it: rowData.nome_it,
								prezzo_cent: rowData.prezzo_cent,
								sconto: rowData.sconto,
								testo_condizioni: rowData.testo_condizioni,
							});
							setOpenEdit(true);
						}}
						variant="ghost"
					>
						<Cog className="text-neutral-600" />
					</Button>
				);
			},
			enableHiding: false,
			enableSorting: false,
			header: ({ column }) => (
				<DataTableColumnHeader column={column} title={columns_titles.actions} />
			),
		},
	];
	const pinnedFiltri: PinnedFiltro[] = [
		{
			columnName: "categoria",
			options: cat_options,
			title: "Categoria",
		},
	];
	const searchFiltro: SearchFiltro = {
		columnName: "id",
		placeholder: "Filtra codice...",
	};
	return (
		<div className="mx-auto w-full px-2 py-10">
			<DataTable
				columns={columns}
				columns_titles={columns_titles}
				data={tabledata}
				defaultSort={[{ desc: false, id: "id" }]}
				pinnedFiltri={pinnedFiltri}
				searchColumn={searchFiltro}
			/>
		</div>
	);
};
