import type { ColumnDef } from "@tanstack/react-table";
import { DataTableColumnHeader } from "~/components/custom_ui/dataTable-header";
import { Cog } from "lucide-react";
import { DataTable } from "~/components/custom_ui/data-table";
import type { SearchFiltro } from "~/components/custom_ui/dataTable-toolbar";
import { Button } from "~/components/ui/button";
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 {
      id: row.id,
      type: row.type,
      value: row.value,
      actions: "Azioni",
    };
  });

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

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

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

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