import { NextResponse } from "next/server"
import { sql } from "@/lib/db"

export async function GET(request: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id: userId } = await params

    // Get all artists the user is following
    const followedArtists = await sql`
      SELECT 
        af.artist_id,
        af.created_at as followed_at,
        a.name,
        a.avatar_url,
        a.bio,
        a.is_verified,
        a.total_followers,
        a.monthly_listeners
      FROM artist_followers af
      JOIN artists a ON a.id = af.artist_id
      WHERE af.user_id = ${userId}
      ORDER BY af.created_at DESC
    `

    return NextResponse.json({
      data: followedArtists,
      total: followedArtists.length,
    })
  } catch (error: any) {
    console.error("Get following error:", error)
    if (error?.message?.includes("Too Many") || error?.status === 429) {
      return NextResponse.json({ data: [], total: 0, rateLimited: true })
    }
    return NextResponse.json({ data: [], total: 0, error: "Failed to get following" })
  }
}
