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

export async function DELETE(request: Request, { params }: { params: Promise<{ id: string; trackId: string }> }) {
  try {
    const { id: playlistId, trackId } = await params

    await sql`
      DELETE FROM playlist_tracks 
      WHERE playlist_id = ${playlistId} AND track_id = ${trackId}
    `

    // Update playlist track count
    await sql`
      UPDATE playlists 
      SET total_tracks = (
        SELECT COUNT(*) FROM playlist_tracks WHERE playlist_id = ${playlistId}
      ),
      updated_at = NOW()
      WHERE id = ${playlistId}
    `

    return NextResponse.json({ success: true })
  } catch (error) {
    console.error("Remove track from playlist error:", error)
    return NextResponse.json({ error: "Erreur lors de la suppression du titre" }, { status: 500 })
  }
}
