import { Button } from "@/shared/common/ui/button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/shared/common/ui/dropdown-menu";
import { MainDTO } from "@/super-admin/dto/SuperAdminDto";
import { ColumnDef } from "@tanstack/react-table";
import { Eye, MoreHorizontal } from "lucide-react";
import { TFunction } from "i18next";
import { NavigateFunction } from "react-router-dom";

export const MigratedDataColumnDefn = (
  localizedName: (name?: { am?: string; en?: string }) => string,
  t: TFunction,
  navigate: NavigateFunction,
): ColumnDef<MainDTO>[] => {
  const handleViewRecord = (recordId: string) => {
    navigate(`/user-management/migrated-records-management/view/${recordId}`);
  };

  return [
    {
      id: "referenceNumber",
      accessorKey: "record.referenceNumber",
      header: t("migration.columns.referenceNumber"),
      cell: ({ row }) => row.original?.record?.referenceNumber,
    },
    {
      id: "letterNumber",
      accessorKey: "record.letterNumber",
      header: t("migration.columns.letterNumber"),
      cell: ({ row }) => row.original?.record?.letterNumber,
    },
    {
      id: "uploadedBy",
      accessorKey: "record.metadata.uploadedBy.en",
      header: t("migration.columns.uploadedBy"),
      cell: ({ row }) =>
        localizedName(
          row.original?.record?.metadata?.uploadedBy ?? { en: "-", am: "-" },
        ),
    },
    {
      id: "organization",
      accessorKey: "record.metadata.organizationName.en",
      header: t("migration.columns.organization"),
      cell: ({ row }) =>
        localizedName(
          row.original?.record?.metadata?.organizationName ?? {
            en: "-",
            am: "-",
          },
        ),
    },
    {
      id: "dispatchedDate",
      accessorKey: "record.dispatchedDate",
      header: t("migration.columns.dispatchedDate"),
      cell: ({ row }) =>
        new Date(row.original?.record?.dispatchedDate).toLocaleString(),
    },
    {
      id: "status",
      accessorKey: "status",
      header: t("migration.columns.status"),
      cell: ({ row }) => row.original?.status,
    },
    {
      id: "subject",
      accessorKey: "record.content",
      header: t("migration.columns.subject"),
      cell: ({ row }) => row.original?.record?.content[0]?.subject || "-",
    },
    {
      id: "actions",
      cell: ({ row }) => {
        const record = row.original.record;
        return (
          <DropdownMenu>
            <DropdownMenuTrigger asChild>
              <Button variant="ghost" size="sm">
                <MoreHorizontal className="h-4 w-4" />
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent align="end">
              <DropdownMenuItem onClick={() => handleViewRecord(record.id)}>
                <Eye className="h-4 w-4 mr-2" />
                {t("migration.viewRecord")}
              </DropdownMenuItem>
            </DropdownMenuContent>
          </DropdownMenu>
        );
      },
    },
  ];
};
