"use client" import { useState, useEffect } from "react" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Input } from "@/components/ui/input" import { Target, CheckCircle, Trash2, Plus, Trophy, Music, Star } from "lucide-react" import { getApiUrl } from "@/lib/api-config" import Link from "next/link" interface ChaseSong { id: number song_id: number song_title: string priority: number notes: string | null created_at: string caught_at: string | null caught_show_id: number | null caught_show_date: string | null } interface ChaseSongsListProps { userId?: number } export function ChaseSongsList({ userId }: ChaseSongsListProps) { const [chaseSongs, setChaseSongs] = useState([]) const [loading, setLoading] = useState(true) const [newSongQuery, setNewSongQuery] = useState("") const [searchResults, setSearchResults] = useState([]) const [showSearch, setShowSearch] = useState(false) useEffect(() => { fetchChaseSongs() }, []) const fetchChaseSongs = async () => { const token = localStorage.getItem("token") if (!token) { setLoading(false) return } try { const res = await fetch(`${getApiUrl()}/chase/songs`, { headers: { Authorization: `Bearer ${token}` } }) if (res.ok) { const data = await res.json() setChaseSongs(data) } } catch (err) { console.error("Failed to fetch chase songs", err) } finally { setLoading(false) } } const searchSongs = async (query: string) => { if (query.length < 2) { setSearchResults([]) return } try { const res = await fetch(`${getApiUrl()}/search/songs?q=${encodeURIComponent(query)}`) if (res.ok) { const data = await res.json() setSearchResults(data.slice(0, 5)) } } catch (err) { console.error("Failed to search songs", err) } } const addChaseSong = async (songId: number) => { const token = localStorage.getItem("token") if (!token) return try { const res = await fetch(`${getApiUrl()}/chase/songs`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, body: JSON.stringify({ song_id: songId, priority: 1 }) }) if (res.ok) { fetchChaseSongs() setNewSongQuery("") setSearchResults([]) setShowSearch(false) } } catch (err) { console.error("Failed to add chase song", err) } } const removeChaseSong = async (chaseId: number) => { const token = localStorage.getItem("token") if (!token) return try { const res = await fetch(`${getApiUrl()}/chase/songs/${chaseId}`, { method: "DELETE", headers: { Authorization: `Bearer ${token}` } }) if (res.ok) { setChaseSongs(chaseSongs.filter(cs => cs.id !== chaseId)) } } catch (err) { console.error("Failed to remove chase song", err) } } const activeChaseSongs = chaseSongs.filter(cs => !cs.caught_at) const caughtChaseSongs = chaseSongs.filter(cs => cs.caught_at) const getPriorityLabel = (priority: number) => { switch (priority) { case 1: return { label: "Must See", color: "bg-red-500/10 text-red-500 border-red-500/30" } case 2: return { label: "Want", color: "bg-yellow-500/10 text-yellow-500 border-yellow-500/30" } default: return { label: "Nice", color: "bg-muted text-muted-foreground" } } } if (loading) { return
Loading chase songs...
} return ( Chase Songs {showSearch && (
{ setNewSongQuery(e.target.value) searchSongs(e.target.value) }} /> {searchResults.length > 0 && (
{searchResults.map((song) => (
addChaseSong(song.id)} > {song.title}
))}
)}
)} {activeChaseSongs.length === 0 && caughtChaseSongs.length === 0 ? (

No chase songs yet. Add songs you want to catch!

) : ( <> {/* Active Chase Songs */} {activeChaseSongs.length > 0 && (

Chasing ({activeChaseSongs.length})

{activeChaseSongs.map((cs) => { const priority = getPriorityLabel(cs.priority) return (
{cs.song_title} {priority.label}
) })}
)} {/* Caught Songs */} {caughtChaseSongs.length > 0 && (

Caught ({caughtChaseSongs.length})

{caughtChaseSongs.map((cs) => (
{cs.song_title} {cs.caught_show_date && ( {new Date(cs.caught_show_date).toLocaleDateString()} )}
))}
)} )}
) }