"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, Search } from "lucide-react"
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog"
import { deleteStory } from "app/lib/del"
import Link from "next/link"


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 <div className="flex items-center justify-around">
        <Link href={`/story/${row.original.id}`}><Search /></Link>
        <Dialog>
          <DialogTrigger asChild>
            <Button variant="ghost"><Trash2 color="red" /></Button>
          </DialogTrigger>
          <DialogContent>
            <DialogHeader>
              <DialogTitle>Are you sure?</DialogTitle>
              <DialogDescription>
                Deleting a story cannot be undone!
              </DialogDescription>
            </DialogHeader>
            <DialogFooter>
              <Button variant="destructive"
                onClick={() => {
                  deleteStory(row.original.id)
                }}>Yes, delete it!
              </Button>
            </DialogFooter>
          </DialogContent>
        </Dialog>
      </div>
    }
  }

]

