import type { CampaignAnalyticsSummary } from "@/lib/features/notifications/action";
import ThemedStatTiles, { type ThemedTileSpec } from "./ThemedStatTiles";
import {
  IconBell,
  IconDeviceMobileCheck,
  IconEye,
  IconHandFinger,
  IconSend,
} from "@tabler/icons-react";
import { useMemo } from "react";

const percentOf = (part: number, whole: number) =>
  whole > 0 ? Math.min(100, (part / whole) * 100) : 0;

const NotificationStatTiles: React.FC<{
  summary: CampaignAnalyticsSummary;
  windowLabel: string;
  loading: boolean;
}> = ({ summary, windowLabel, loading }) => {
  const tiles = useMemo<ThemedTileSpec[]>(
    () => [
      {
        key: "campaigns",
        label: "Notifications Sent",
        value: summary.campaignsSent,
        icon: <IconBell size={18} stroke={1.8} />,
        hint: windowLabel,
      },
      {
        key: "sends",
        label: "Sends",
        value: summary.sends,
        icon: <IconSend size={18} stroke={1.8} />,
        hint: "Push notifications dispatched",
      },
      {
        key: "received",
        label: "Received",
        value: summary.received,
        icon: <IconDeviceMobileCheck size={18} stroke={1.8} />,
        percent: percentOf(summary.received, summary.sends),
        hint: `${percentOf(summary.received, summary.sends).toFixed(1)}% of sends delivered`,
      },
      {
        key: "impressions",
        label: "Impressions",
        value: summary.impressions,
        icon: <IconEye size={18} stroke={1.8} />,
        hint: "Shown on device lockscreens",
      },
      {
        key: "opens",
        label: "Opens",
        value: summary.opens,
        icon: <IconHandFinger size={18} stroke={1.8} />,
        percent: percentOf(summary.opens, summary.received),
        hint: `${percentOf(summary.opens, summary.received).toFixed(1)}% open rate`,
      },
    ],
    [summary, windowLabel],
  );

  return <ThemedStatTiles tiles={tiles} loading={loading} />;
};

export default NotificationStatTiles;
