import { getSession } from '@/lib/session'
import { checkTradeCode } from '@/lib/trade'

/** COMPRA: consulta el valor en PD y el coste en oro de un código. */
export async function POST(request: Request) {
  const session = await getSession()
  if (!session.accountId || !session.username) {
    return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })
  }

  let body: Record<string, unknown> = {}
  try {
    body = await request.json()
  } catch {
    return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })
  }

  return Response.json(await checkTradeCode(String(body.code ?? '')))
}
