import { ColumnDef } from "@tanstack/react-table";
import { format } from "date-fns";
import { useLocalizedName } from "@/shared/common/localizedName";
import { PositionTypeDto } from "@/user-management/dto/positions/positionType";
import PositionTypeActionsCell from "./PositionTypeActions";
import { t } from "i18next";

interface PositionTypeResponse {
  items: PositionTypeDto[];
  count: number;
}

const NameCell = ({ name }: { name: PositionTypeDto["name"] }) => {
  const localizedName = useLocalizedName();
  return <span>{localizedName(name)}</span>;
};

export const createPositionTypeColumns = (
  globalPositionTypes?: PositionTypeResponse,
  onDelete?: () => void | Promise<void>,
  onEdit?: () => void | Promise<void>,
): ColumnDef<PositionTypeDto>[] => [
  {
    accessorKey: "name",
    header: () => t("common.name"),
    cell: ({ row }) => <NameCell name={row.original.name} />,
  },
  {
    accessorKey: "key",
    header: () => t("contentManagement.key"),
    cell: ({ row }) => <span>{row.original.key}</span>,
  },
  {
    accessorKey: "createdAt",
    header: () => t("contentManagement.createdAt"),
    cell: ({ row }) => (
      <span>
        {format(new Date(row.original.createdAt), "MMM d, yyyy HH:mm")}
      </span>
    ),
  },
  {
    accessorKey: "updatedAt",
    header: () => t("contentManagement.updatedAt"),
    cell: ({ row }) => (
      <span>
        {format(new Date(row.original.updatedAt), "MMM d, yyyy HH:mm")}
      </span>
    ),
  },
  {
    id: "actions",
    header: () => t("userRecord.Actions"),
    cell: ({ row }) => (
      <PositionTypeActionsCell
        row={row.original}
        globalPositionTypes={globalPositionTypes}
        onDelete={onDelete}
        onEdit={onEdit}
      />
    ),
  },
];

export const PositionTypeColumnDefn = createPositionTypeColumns();
