import { VERTICALS } from "@/contexts/vertical-context"
import { notFound } from "next/navigation"
import { getApiUrl } from "@/lib/api-config"

interface Props {
    params: { vertical: string }
}

export function generateStaticParams() {
    return VERTICALS.map((v) => ({
        vertical: v.slug,
    }))
}

async function getSongs(verticalSlug: string) {
    try {
        const res = await fetch(`${getApiUrl()}/songs?vertical=${verticalSlug}`, {
            next: { revalidate: 60 }
        })
        if (!res.ok) return []
        return res.json()
    } catch {
        return []
    }
}

export default async function SongsPage({ params }: Props) {
    const vertical = VERTICALS.find((v) => v.slug === params.vertical)

    if (!vertical) {
        notFound()
    }

    const songs = await getSongs(vertical.slug)

    return (
        <div className="space-y-6">
            <div className="flex items-center justify-between">
                <h1 className="text-3xl font-bold">
                    <span className="mr-2">{vertical.emoji}</span>
                    {vertical.name} Songs
                </h1>
            </div>

            {songs.length === 0 ? (
                <div className="text-center py-12 text-muted-foreground">
                    <p>No songs imported yet for {vertical.name}.</p>
                    <p className="text-sm mt-2">Run the data importer to populate songs.</p>
                </div>
            ) : (
                <div className="grid gap-2">
                    {songs.map((song: any) => (
                        <a
                            key={song.id}
                            href={`/${vertical.slug}/songs/${song.slug}`}
                            className="flex justify-between items-center p-3 rounded-lg border bg-card hover:bg-accent transition-colors"
                        >
                            <div>
                                <div className="font-medium">{song.title}</div>
                                {song.original_artist && (
                                    <div className="text-sm text-muted-foreground">
                                        Cover of {song.original_artist}
                                    </div>
                                )}
                            </div>
                            <div className="text-sm text-muted-foreground">
                                {song.times_played || 0} plays
                            </div>
                        </a>
                    ))}
                </div>
            )}
        </div>
    )
}
