import React, { useState } from 'react';
import { type Recipe } from "../types/Recipe"
import Modal from '../components/Modal.tsx'

interface CookbookRecipeTileProps {
  recipe: Recipe;
  handleDelete: (id: number | undefined) => void;
}

function CookbookRecipeTile({ recipe, handleDelete }: CookbookRecipeTileProps) {
  const [isModalOpen, setIsModalOpen] = useState(false);

  const openModal = () => setIsModalOpen(true);
  const closeModal = () => setIsModalOpen(false);
  const confirmDelete = () => {
    handleDelete(recipe.id);
    closeModal();
  };


  return (
    <div className="recipe-card">
      <div className="recipe-info">
        <h3><a href={`/recipe/${recipe.id}`}>{recipe.name}</a></h3>
        <button onClick={openModal}>Delete Recipe</button>
      </div>

      <Modal
        isOpen={isModalOpen}
        onClose={closeModal}
        message="Are you sure you want to delete this recipe?"
        confirmAction={confirmDelete}
        cancelAction={closeModal}
      />
    </div>
  );
}

export default CookbookRecipeTile;
