import type { initialValues } from "~/forms/FormFlags";
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 { Flags } from "~/schemas/public/Flags";
export const FlagsTable = (props: {
  data: Flags[];
  setOpenEdit: (status: boolean) => void;
  setEditData: (data: initialValues | null) => void;
}) => {
  const { data, setOpenEdit, setEditData } = props;

  const tabledata = data.map((row) => {
    return {
      id: row.id,
      value: row.value,

      actions: "Azioni",
    };
  });

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

  const columns: ColumnDef<Column>[] = [
    {
      header: ({ column }) => (
        <DataTableColumnHeader column={column} title={columns_titles.id} />
      ),
      accessorKey: "id",
      enableHiding: false,
    },
    {
      header: ({ column }) => (
        <DataTableColumnHeader column={column} title={columns_titles.value} />
      ),
      accessorKey: "value",
      cell: ({ row }) => {
        const data = row.original;
        return data.value === null ? "null" : data.value;
      },
      enableHiding: false,
    },

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

  const searchFiltro: SearchFiltro = {
    columnName: "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: "id" }]}
      />
    </div>
  );
};
