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 { TestiEStringhe } from "~/schemas/public/TestiEStringhe";

export const StringheTable = ({
  data,
  setOpenEdit,
  setEditData,
}: {
  data: TestiEStringhe[];
  setOpenEdit: (status: boolean) => void;
  setEditData: (data: TestiEStringhe | null) => void;
}) => {
  const tabledata = data.map((row) => {
    return {
      stinga_id: row.stinga_id,
      stringa_value: row.stringa_value,

      actions: "Azioni",
    };
  });

  const columns_titles = {
    stinga_id: "ID",
    stringa_value: "Testo",
    actions: "Azioni",
  };
  type Column = (typeof tabledata)[number];

  const columns: ColumnDef<Column>[] = [
    {
      header: ({ column }) => (
        <DataTableColumnHeader
          column={column}
          title={columns_titles.stinga_id}
        />
      ),
      accessorKey: "stinga_id",
      enableHiding: false,
    },
    {
      header: ({ column }) => (
        <DataTableColumnHeader
          column={column}
          title={columns_titles.stringa_value}
        />
      ),
      accessorKey: "stringa_value",
      cell: ({ row }) => {
        const data = row.original;
        return <div className="max-w-xs truncate">{data.stringa_value}</div>;
      },
      enableHiding: false,
    },

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

  const searchFiltro: SearchFiltro = {
    columnName: "stinga_id",
    placeholder: "Filtra codice...",
  };
  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: "stinga_id" }]}
      />
    </div>
  );
};
