import * as React from "react";
import { ColumnDef } from "@tanstack/react-table";

import { Eye, MoreVertical, SquarePen, Trash2 } from "lucide-react";
import { cn } from "@/lib/utils";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuTrigger,
  DropdownMenuItem,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { format } from "date-fns";

const columns: ColumnDef<any>[] = [
  {
    accessorKey: "no",
    header: "No",
    cell: ({ row }) => <span>{row.getValue("no")}</span>,
  },
  {
    accessorKey: "title",
    header: "Title",
    cell: ({ row }) => (
      <span className="whitespace-normal">{row.getValue("title")}</span>
    ),
  },
  {
    accessorKey: "categoryName",
    header: "Category",
    cell: ({ row }) => <span>{row.getValue("categoryName")}</span>,
  },
  {
    accessorKey: "createdAt",
    header: "Upload Date",
    cell: ({ row }) => {
      const createdAt = row.getValue("createdAt") as
        | string
        | number
        | undefined;

      const formattedDate =
        createdAt && !isNaN(new Date(createdAt).getTime())
          ? format(new Date(createdAt), "dd-MM-yyyy HH:mm:ss")
          : "-";
      return <span className="whitespace-nowrap">{formattedDate}</span>;
    },
  },
  {
    accessorKey: "tags",
    header: "Tag",
    cell: ({ row }) => <span className="">{row.getValue("tags")}</span>,
  },
  {
    accessorKey: "statusName",
    header: "Status",
    cell: ({ row }) => {
      const statusColors: Record<string, string> = {
        diterima: "bg-green-100 text-green-600",
        "menunggu review": "bg-orange-100 text-orange-600",
      };

      // Mengambil `statusName` dari data API
      const status = row.getValue("statusName") as string;
      const statusName = status?.toLocaleLowerCase(); // Ubah ke huruf kecil

      // Gunakan `statusName` untuk pencocokan
      const statusStyles =
        statusColors[statusName] || "bg-gray-100 text-gray-600";

      return (
        <Badge className={cn("rounded-full px-5", statusStyles)}>
          {status} {/* Tetap tampilkan nilai asli */}
        </Badge>
      );
    },
  },

  {
    id: "actions",
    accessorKey: "action",
    header: "Actions",
    enableHiding: false,
    cell: ({ row }) => {
      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button
              size="icon"
              className="bg-transparent ring-offset-transparent hover:bg-transparent hover:ring-0 hover:ring-transparent"
            >
              <span className="sr-only">Open menu</span>
              <MoreVertical className="h-4 w-4 text-default-800" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent className="p-0" align="end">
            <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
              <Eye className="w-4 h-4 me-1.5" />
              View
            </DropdownMenuItem>
            <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
              <SquarePen className="w-4 h-4 me-1.5" />
              Edit
            </DropdownMenuItem>
            <DropdownMenuItem className="p-2 border-b text-destructive bg-destructive/30  focus:bg-destructive focus:text-destructive-foreground rounded-none">
              <Trash2 className="w-4 h-4 me-1.5" />
              Delete
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      );
    },
  },
];

export default columns;
