import { Edit2, Trash2, Eye, Users, MoreVertical, History } from "lucide-react";
import { ActionIcon, Menu, MenuItem, Tooltip } from "@mantine/core";
import { useNavigate } from "react-router-dom";

import type { FleetResourceConfig } from "@/pages/fleet/config/resources";
import type { FleetRecord } from "@/services/fleet/fleet.service";

export interface FleetRecordActionsProps {
  record: FleetRecord;
  config: FleetResourceConfig;
  /** Omit to hide the action (caller lacks the update/delete permission). */
  onEdit?: (record: FleetRecord) => void;
  onRemove?: (record: FleetRecord) => void;
  onAssignDriver?: (record: FleetRecord) => void;
  onHistory?: (record: FleetRecord) => void;
  onViewDetail?: (record: FleetRecord) => void;
  layout?: "row" | "compact";
}

const FleetRecordActions = ({
  record,
  config,
  onEdit,
  onRemove,
  onAssignDriver,
  onHistory,
  onViewDetail,
  layout = "row",
}: FleetRecordActionsProps) => {
  const navigate = useNavigate();
  const removeLabel = config.removeActionLabel ?? "Delete";
  const showDetail = Boolean(config.detailPath && "id" in record);
  const showViewDetail = Boolean(onViewDetail);
  const isVehicle = config.slug === "vehicles";
  const showHistory =
    Boolean(onHistory) &&
    (config.slug === "drivers" ||
      config.slug === "vehicles" ||
      config.slug === "wagons");

  const handleDetail = () => {
    if (!config.detailPath || !("id" in record)) return;
    navigate(config.detailPath.replace(":id", String(record.id)));
  };

  if (
    !onEdit &&
    !onRemove &&
    !showDetail &&
    !showViewDetail &&
    !showHistory &&
    !(isVehicle && onAssignDriver)
  ) {
    return null;
  }

  if (layout === "compact") {
    return (
      <Menu position="bottom-end" withinPortal shadow="md">
        <Menu.Target>
          <Tooltip label="Actions">
            <ActionIcon variant="subtle" color="gray" size="sm">
              <MoreVertical size={16} strokeWidth={2} />
            </ActionIcon>
          </Tooltip>
        </Menu.Target>
        <Menu.Dropdown>
          {isVehicle && onAssignDriver ? (
            <MenuItem
              onClick={() => onAssignDriver(record)}
              leftSection={<Users size={14} strokeWidth={2} />}
            >
              Assign Driver
            </MenuItem>
          ) : null}
          {onEdit ? (
            <MenuItem
              onClick={() => onEdit(record)}
              leftSection={<Edit2 size={14} strokeWidth={2} />}
            >
              Edit
            </MenuItem>
          ) : null}
          {showViewDetail ? (
            <MenuItem
              onClick={() => onViewDetail?.(record)}
              leftSection={<Eye size={14} strokeWidth={2} />}
            >
              View detail
            </MenuItem>
          ) : null}
          {showHistory ? (
            <MenuItem
              onClick={() => onHistory?.(record)}
              leftSection={<History size={14} strokeWidth={2} />}
            >
              History
            </MenuItem>
          ) : null}
          {showDetail ? (
            <MenuItem
              onClick={handleDetail}
              leftSection={<Eye size={14} strokeWidth={2} />}
            >
              View details
            </MenuItem>
          ) : null}
          {onRemove ? (
            <MenuItem
              color="red"
              onClick={() => onRemove(record)}
              leftSection={<Trash2 size={14} strokeWidth={2} />}
            >
              {removeLabel}
            </MenuItem>
          ) : null}
        </Menu.Dropdown>
      </Menu>
    );
  }

  return (
    <Menu position="bottom-end" withinPortal shadow="md">
      <Menu.Target>
        <Tooltip label="Actions">
          <ActionIcon variant="subtle" color="gray" size="sm">
            <MoreVertical size={16} strokeWidth={2} />
          </ActionIcon>
        </Tooltip>
      </Menu.Target>
      <Menu.Dropdown>
        {isVehicle && onAssignDriver ? (
          <MenuItem
            onClick={() => onAssignDriver(record)}
            leftSection={<Users size={14} strokeWidth={2} />}
          >
            Assign Driver
          </MenuItem>
        ) : null}
        {onEdit ? (
          <MenuItem
            onClick={() => onEdit(record)}
            leftSection={<Edit2 size={14} strokeWidth={2} />}
          >
            Edit
          </MenuItem>
        ) : null}
        {showViewDetail ? (
          <MenuItem
            onClick={() => onViewDetail?.(record)}
            leftSection={<Eye size={14} strokeWidth={2} />}
          >
            View detail
          </MenuItem>
        ) : null}
        {showDetail ? (
          <MenuItem
            onClick={handleDetail}
            leftSection={<Eye size={14} strokeWidth={2} />}
          >
            View details
          </MenuItem>
        ) : null}
        {onRemove ? (
          <MenuItem
            color="red"
            onClick={() => onRemove(record)}
            leftSection={<Trash2 size={14} strokeWidth={2} />}
          >
            {removeLabel}
          </MenuItem>
        ) : null}
      </Menu.Dropdown>
    </Menu>
  );
};

export default FleetRecordActions;
