"use client"
import { ColumnDef, createColumnHelper } from "@tanstack/react-table"
import { StoryWithGenres } from "./page"
import { ArrowUpDown, BookType, Drama, Tally5 } from "lucide-react"
import { Button } from "@/components/ui/button"
import { selectCol } from "app/ui/tables/selectColumn"
import NumberInputCell from "app/ui/tables/inputs/numberInput"
import { storySchema } from "app/ui/forms/schemas"
import { TextInputCell } from "app/ui/tables/inputs/textInput"
import GenrePickerInputCell from "app/ui/tables/inputs/genrePickerInput"
const columnHelper = createColumnHelper<StoryWithGenres>()

export const columns: ColumnDef<StoryWithGenres>[] = [
  selectCol,
  {
    accessorKey: "title",
    header: ({ column }) => {
      return (
        <Button
          variant="ghost"
          className="px-1"
          onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
        >
          <span className="hidden sm:block">
            Title
          </span>
          <span className="block sm:hidden"><BookType /></span>
          <ArrowUpDown className="ml-2 h-4 w-4" />
        </Button>
      )
    },
    cell: cell => (
      <>
        <p className="block break-words max-w-36 md:hidden text-xs">{cell.getValue()}</p>
        <TextInputCell cellContext={cell} className="hidden md:block" />
      </>
    ),
    meta: { formSchema: storySchema }
  },
  {
    accessorKey: "word_count",
    header: ({ column }) => {
      return (
        <Button
          variant="ghost"
          className="px-1"
          onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
        >
          <span className="hidden sm:block">
            Word Count
          </span>
          <span className="sm:hidden">
            <Tally5 />
          </span>
          <ArrowUpDown className="ml-2 h-4 w-4" />
        </Button>
      )
    },
    enableColumnFilter: false,
    cell: cell => (
      <>
        <p className="block md:hidden text-center text-xs">{cell.getValue()}</p>
        <NumberInputCell cellContext={cell} className="hidden md:block" />
      </>
    ),
    meta: {
      step: 50,
      formSchema: storySchema
    }
  },
  columnHelper.accessor("genres", {
    header: () => (
      <div className="w-fit mx-auto">
        <span className="hidden sm:block">Genres</span>
        <span className="sm:hidden"><Drama /></span>
      </div>
    ),
    cell: GenrePickerInputCell,
    filterFn: "arrIncludes",
    meta: {}
  }),

]

