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

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

    const tracks = await sql`
      SELECT t.*, a.name as artist_name, dt.quality, dt.file_size_bytes
      FROM downloaded_tracks dt
      JOIN tracks t ON dt.track_id = t.id
      LEFT JOIN artists a ON t.artist_id = a.id
      WHERE dt.user_id = ${id}
      ORDER BY dt.created_at DESC
    `

    return NextResponse.json({ tracks })
  } catch (error) {
    console.error("Get downloads error:", error)
    return NextResponse.json({ error: "Erreur lors de la recuperation" }, { status: 500 })
  }
}
