"use client"
import { CellContext, ColumnDef, createColumnHelper } from "@tanstack/react-table"
import { ArrowUpDown } from "lucide-react"
import { Button } from "@/components/ui/button"
import { SubComplete } from "./page"
import { selectCol } from "app/ui/tables/selectColumn"
import TitleContainer from "app/ui/titleContainer"



export const columns: ColumnDef<SubComplete>[] = [
  selectCol,
  {
    accessorFn: row => {
      if (row.story) {
        return row.story.title
      }
      return "RECORD DELETED"
    },
    id: "story",
    header: "Story",
    cell: (props: CellContext<any, any>) => (<TitleContainer>{props.getValue()}</TitleContainer>)
  },
  {
    accessorFn: row => {
      if (row.pub) {
        return row.pub.title
      }
      return "RECORD DELETED"
    },
    id: "pub",
    header: "Publication",
    cell: (props: CellContext<any, any>) => (<TitleContainer>{props.getValue()}</TitleContainer>)
  },
  {
    accessorFn: row => new Date(row.submitted),
    id: "submitted",
    header: ({ column }) => {
      return (
        <Button
          variant="ghost"
          onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
        >
          Date Submitted
          <ArrowUpDown className="ml-2 h-4 w-4" />
        </Button>
      )
    },
    enableColumnFilter: false,
    sortingFn: "datetime",
    cell: (props: CellContext<any, any>) => (<p className="w-full text-center">{props.getValue().toLocaleDateString()}</p>)
  },
  {
    accessorFn: row => row.responded ? new Date(row.responded) : null,
    id: "responded",
    header: ({ column }) => {
      return (
        <Button
          variant="ghost"
          onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
        >
          Date Responded
          <ArrowUpDown className="ml-2 h-4 w-4" />
        </Button>
      )
    },
    enableColumnFilter: false,
    sortingFn: "datetime",
    cell: (props: CellContext<any, any>) => (<p className="w-full text-center">{props.getValue()?.toLocaleDateString()}</p>)
  },
  {
    accessorFn: row => {
      if (row.response) {
        return row.response.response
      }
      return "RECORD DELETED"
    },
    id: "response",
    header: "Response",
    cell: (props: CellContext<any, any>) => (<p className="w-full text-center">{props.getValue()}</p>)
  },

]

