"use client"

import { useEffect, useState } from "react"
import { getApiUrl } from "@/lib/api-config"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import Link from "next/link"
import { MapPin } from "lucide-react"

interface Venue {
    id: number
    name: string
    city: string
    state: string
    country: string
}

export default function VenuesPage() {
    const [venues, setVenues] = useState<Venue[]>([])
    const [loading, setLoading] = useState(true)

    useEffect(() => {
        fetch(`${getApiUrl()}/venues/?limit=100`)
            .then(res => res.json())
            .then(data => {
                // Sort alphabetically
                const sorted = data.sort((a: Venue, b: Venue) => a.name.localeCompare(b.name))
                setVenues(sorted)
            })
            .catch(console.error)
            .finally(() => setLoading(false))
    }, [])

    if (loading) return <div className="container py-10">Loading venues...</div>

    return (
        <div className="container py-10 space-y-8">
            <div className="flex flex-col gap-2">
                <h1 className="text-3xl font-bold tracking-tight">Venues</h1>
                <p className="text-muted-foreground">
                    Explore the iconic venues where the magic happens.
                </p>
            </div>

            <div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
                {venues.map((venue) => (
                    <Link key={venue.id} href={`/venues/${venue.id}`}>
                        <Card className="h-full hover:bg-accent/50 transition-colors">
                            <CardHeader>
                                <CardTitle className="flex items-center gap-2">
                                    <MapPin className="h-5 w-5 text-green-500" />
                                    {venue.name}
                                </CardTitle>
                            </CardHeader>
                            <CardContent>
                                <p className="text-muted-foreground">
                                    {venue.city}, {venue.state}
                                </p>
                            </CardContent>
                        </Card>
                    </Link>
                ))}
            </div>
        </div>
    )
}
