"use client"
import { ColumnDef, createColumnHelper } from "@tanstack/react-table"
import { StoryWithGenres } from "./page"
import { ArrowUpDown, MoreHorizontal } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Badge } from "@/components/ui/badge"
import { Trash2 } from "lucide-react"
import { deleteStory } from "app/lib/del"


const columnHelper = createColumnHelper<StoryWithGenres>()


export const columns: ColumnDef<StoryWithGenres>[] = [
  {
    accessorKey: "title",
    header: ({ column }) => {
      return (
        <Button
          variant="ghost"
          onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
        >
          Title
          <ArrowUpDown className="ml-2 h-4 w-4" />
        </Button>
      )
    },

  },
  {
    accessorKey: "word_count",
    header: ({ column }) => {
      return (
        <Button
          variant="ghost"
          onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
        >
          Word Count
          <ArrowUpDown className="ml-2 h-4 w-4" />
        </Button>
      )
    },
    enableColumnFilter: false
  },
  columnHelper.accessor("genres", {
    cell: props => {
      const genres = props.getValue()
        .map(e => <Badge>{e.name}</Badge>)
      return genres
    }
  }),
  {
    id: "actions",
    header: "Actions",
    cell: ({ row }) => {
      return <Button variant="ghost"
        onClick={() => { deleteStory(row.original.id) }}>
        <Trash2 color="red" /></Button>
    }
  }

]

