import { DelegatePositionDto } from "@/record-management/dto/delegation/delegationDto";
import { ColumnDef } from "@tanstack/react-table";
import { t } from "i18next";
import { renderStatus } from "@/record-management/utils/renderDetails";
import i18n from "i18next";
import { DelegationRecordActions } from "./delegationColumnAction";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@/shared/common/ui/tooltip";

export const MydelegateeColumn = (
  refetch: () => void
): ColumnDef<DelegatePositionDto>[] => [
  {
    accessorKey: "delegateePosition",
    header: () => t("delegation.delegateePosition"), //Delegated Position
    enableHiding: true,
    cell: ({ row }) => {
      const isAmharic = i18n.language === "am";
      const positionName = isAmharic
        ? row.original.position?.name.am
        : row.original.position?.name.en;

      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help">
                {positionName}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{positionName}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    },
  },
  {
    accessorKey: "delegateeEmployee",
    header: () => t("delegation.delegateeEmployee"), //Delegated Employee
    enableHiding: true,
    cell: ({ row }) => {
      const isAmharic = i18n.language === "am";
      const employeeName = isAmharic
        ? row.original.delegator?.employee?.name.am
        : row.original.delegator?.employee?.name.en;

      return (
        <TooltipProvider>
          <Tooltip>
            <TooltipTrigger asChild>
              <div className="max-w-[200px] truncate cursor-help">
                {employeeName}
              </div>
            </TooltipTrigger>
            <TooltipContent>
              <p>{employeeName}</p>
            </TooltipContent>
          </Tooltip>
        </TooltipProvider>
      );
    },
  },
  {
    accessorKey: "startDate",
    header: () => t("delegation.startDate"),
    enableHiding: true,
    cell: ({ row }) => {
      const isAmharic = i18n.language === "am";
      const startDate = row.original.startDate;
      const amharicStartDate = row.original.amharicStartDate;

      if (isAmharic && amharicStartDate) {
        return <div>{amharicStartDate}</div>;
      }

      const formattedDate = startDate
        ? new Date(startDate).toLocaleDateString("en-US", {
            month: "short",
            day: "numeric",
            year: "numeric",
            hour: "2-digit",
            minute: "2-digit",
          })
        : "-";

      return <div>{formattedDate}</div>;
    },
  },
  {
    accessorKey: "endDate",
    header: () => t("delegation.endDate"),
    enableHiding: true,
    cell: ({ row }) => {
      const isAmharic = i18n.language === "am";
      const endDate = row.original.endDate;
      const amharicEndDate = row.original.amharicEndDate;

      if (isAmharic && amharicEndDate) {
        return <div>{amharicEndDate}</div>;
      }

      const formattedDate = endDate
        ? new Date(endDate).toLocaleString("en-US", {
            month: "short",
            day: "numeric",
            year: "numeric",
            hour: "2-digit",
            minute: "2-digit",
          })
        : "-";

      return <div>{formattedDate}</div>;
    },
  },
  {
    accessorKey: "isCurrent",
    header: () => t("userRecord.Status"),
    cell: ({ row }) =>
      row.original.isCurrent
        ? renderStatus("activated", "activated")
        : renderStatus("deactivated", "deactivated"),
    enableHiding: true,
  },
  {
    id: "Actions",
    header: () => t("userRecord.Actions"),
    accessorKey: "Actions",
    enableHiding: true,
    cell: ({ row }) => {
      const rowData = row.original;

      return (
        <DelegationRecordActions
          rowData={rowData as unknown as DelegatePositionDto}
          refetchTable={refetch}
          type="delegatee"
        />
      );
    },
  },
];
