"use client" import { useState } from "react" import { useMutation } from "@tanstack/react-query" import { useRouter } from "next/navigation" import { Plus, Trash2, Lightbulb } from "lucide-react" import { toast } from "sonner" import { useAuthApi } from "@/shared/useApi" import { confirm } from "@/shared/components/confirm-dialog" import { Button } from "@/shared/components/ui/button" import { Card, CardContent, CardHeader, CardTitle, } from "@/shared/components/ui/card" import { Textarea } from "@/shared/components/ui/textarea" type ShopRecommendation = { id: number recommendation: string created_at?: string } type Props = { jobCardId: string recommendations: ShopRecommendation[] } export function JobCardRecommendationsList({ jobCardId, recommendations }: Props) { const api = useAuthApi() const router = useRouter() const [newRecommendation, setNewRecommendation] = useState("") const addMutation = useMutation({ mutationFn: (recommendation: string) => api.jobCards.addShopRecommendation(jobCardId, { recommendation }), onSuccess: () => { toast.success("Shop recommendation added") setNewRecommendation("") router.refresh() }, onError: () => { toast.error("Failed to add shop recommendation") }, }) const deleteMutation = useMutation({ mutationFn: (recommendationId: number) => api.jobCards.deleteShopRecommendation(jobCardId, recommendationId), onSuccess: () => { toast.success("Shop recommendation deleted") router.refresh() }, onError: () => { toast.error("Failed to delete shop recommendation") }, }) const handleAdd = () => { const trimmed = newRecommendation.trim() if (!trimmed) return addMutation.mutate(trimmed) } const handleDelete = async (recommendation: ShopRecommendation) => { const confirmed = await confirm({ title: "Delete Shop Recommendation", description: "Are you sure you want to delete this recommendation?", confirmLabel: "Delete", variant: "destructive", }) if (confirmed) { deleteMutation.mutate(recommendation.id) } } return ( Shop Recommendations {recommendations.length === 0 && (

No shop recommendations yet.

)} {recommendations.map((rec) => (

{rec.recommendation}

))}