"use client";

import type { WorkoutSet } from "@enode/core/training/models";
import { useTranslation } from "@enode/core/translations/hooks";
import { BottomSheetCard } from "@enode/ui/bottom-sheet-card";
import { Button } from "@enode/ui/button";
import { ChartIcon, TrashIcon, XIcon } from "@enode/ui/icons";

import { getSetTitle } from "./set-list";
import { HEADER_ICON_BUTTON } from "@enode/ui/header-button";

// What a tapped feedback-training set offers: read it in Insights, or delete it.
// Built like the workout's Edit-set sheet — round close button, the set as the
// title, a neutral and a destructive button side by side — minus the form,
// since a feedback set is never saved and has nothing to edit. Delete hands
// back to the flow, which asks for confirmation first.
export function FeedbackSetActionsSheet({
  set,
  allSets,
  onClose,
  onShowInsights,
  onDelete,
}: {
  // The tapped set; null closes the sheet.
  set: WorkoutSet | null;
  // Numbers the title the same way as the row (oldest first).
  allSets: WorkoutSet[];
  onClose: () => void;
  onShowInsights: () => void;
  onDelete: (setID: string) => void;
}) {
  const { t } = useTranslation();
  const title = set ? getSetTitle(set, allSets) : t("Set");

  return (
    <BottomSheetCard open={set !== null} onClose={onClose} ariaLabel={title}>
      <div className="flex items-center px-5 pt-4">
        <button
          type="button"
          onClick={onClose}
          aria-label={t("Close")}
          className={HEADER_ICON_BUTTON}
        >
          <XIcon className="size-5" />
        </button>
      </div>

      <h3 className="heading-portal px-5 pt-3 pb-5">{title}</h3>

      {set && (
        <div className="flex gap-2 px-5">
          <Button
            variant="neutral"
            size="small"
            leadingIcon={<ChartIcon className="size-4" />}
            onClick={onShowInsights}
          >
            {t("Insights")}
          </Button>
          <Button
            variant="destructive"
            size="small"
            leadingIcon={<TrashIcon className="size-4" />}
            onClick={() => onDelete(set.id)}
          >
            {t("Delete")}
          </Button>
        </div>
      )}
    </BottomSheetCard>
  );
}
