"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Target, Check, Loader2 } from "lucide-react" import { getApiUrl } from "@/lib/api-config" import { useAuth } from "@/contexts/auth-context" interface ChaseSong { id: number song_id: number song_title: string caught_at: string | null caught_show_id: number | null } interface MarkCaughtButtonProps { songId: number songTitle: string showId: number className?: string } export function MarkCaughtButton({ songId, songTitle, showId, className }: MarkCaughtButtonProps) { const { user, token } = useAuth() const [chaseSong, setChaseSong] = useState(null) const [loading, setLoading] = useState(false) const [marking, setMarking] = useState(false) useEffect(() => { if (!user || !token) return // Check if this song is in the user's chase list setLoading(true) fetch(`${getApiUrl()}/chase/songs`, { headers: { Authorization: `Bearer ${token}` } }) .then(res => res.ok ? res.json() : []) .then((songs: ChaseSong[]) => { const match = songs.find(s => s.song_id === songId) setChaseSong(match || null) }) .catch(() => setChaseSong(null)) .finally(() => setLoading(false)) }, [user, token, songId]) const handleMarkCaught = async () => { if (!chaseSong || !token) return setMarking(true) try { const res = await fetch(`${getApiUrl()}/chase/songs/${chaseSong.id}/caught?show_id=${showId}`, { method: "POST", headers: { Authorization: `Bearer ${token}` } }) if (!res.ok) throw new Error("Failed to mark caught") // Update local state setChaseSong({ ...chaseSong, caught_at: new Date().toISOString(), caught_show_id: showId }) } catch (err) { console.error(err) alert("Failed to mark song as caught") } finally { setMarking(false) } } // Not logged in or not chasing this song if (!user || !chaseSong) return null // Already caught at THIS show if (chaseSong.caught_show_id === showId) { return ( Caught! ) } // Already caught at another show if (chaseSong.caught_at) { return ( Caught ) } // Chasing but not yet caught return ( ) }