"use client";

import { FC, ReactNode } from "react";
import Link from "@/components/common/versioned-link";
import { ChevronLeft } from "lucide-react";

interface ProfileItemProps {
  icon: ReactNode;
  text: string;
  href: string;
  index: number;
  totalItems: number;
  isExternalLink?: boolean;
  /** Runs instead of following href (e.g. */
  onClick?: () => void;
  badge?: number;
}

const ProfileItem: FC<ProfileItemProps> = ({
  icon,
  text,
  href,
  index,
  totalItems,
  isExternalLink = false,
  onClick,
  badge = 0,
}) => {
  let radius = "";
  if (index === 0) radius = "rounded-t-xl";
  else if (index === totalItems - 1) radius = "rounded-b-xl";

  const baseClasses =
    `flex items-center justify-between px-4 py-3.5 bg-card hover:bg-paper-2/60 transition border-b border-line last:border-b-0 ${radius}`;

  const content = (
    <>
      <div className="flex items-center">
        <span className="text-sienna [&>svg]:h-5 [&>svg]:w-5">{icon}</span>
        <span className="text-body font-bold mr-3">{text}</span>
      </div>
      <span className="flex items-center gap-2">
        {badge > 0 && (
          <span className="min-w-5 h-5 px-1.5 rounded-full bg-sienna text-canvas text-micro font-bold grid place-items-center tabular-nums"
                aria-label={`${badge} خوانده‌نشده`}>
            {badge > 9 ? "۹+" : badge.toLocaleString("fa-IR")}
          </span>
        )}
        <ChevronLeft className="h-5 w-5 text-muted-foreground" />
      </span>
    </>
  );

  if (onClick) {
    return (
      <button type="button" onClick={onClick} className={`w-full text-right ${baseClasses}`}>
        {content}
      </button>
    );
  }

  if (isExternalLink) {
    return (
      <a
        href={href}
        target="_blank"
        rel="noopener noreferrer"
        className={baseClasses}
      >
        {content}
      </a>
    );
  }

  return (
    <Link href={href} className={baseClasses}>
      {content}
    </Link>
  );
};

export default ProfileItem;
