import { NextResponse } from 'next/server'
import { slskdFetch } from '@/lib/slskd'

export async function POST(request: Request) {
  try {
    const { query } = await request.json()
    if (!query) {
      return NextResponse.json({ error: 'query required' }, { status: 400 })
    }

    const res = await slskdFetch('/searches', {
      method: 'POST',
      body: JSON.stringify({ searchText: query }),
    })

    if (!res.ok) {
      throw new Error(`slskd search returned ${res.status}`)
    }

    const data = await res.json()
    return NextResponse.json({ searchId: data.id })
  } catch (error) {
    console.error('Soulseek search error:', error)
    return NextResponse.json({ error: 'Soulseek search failed' }, { status: 502 })
  }
}
