import { getSession } from '@/lib/session'
import { registerVote } from '@/lib/vote'

export async function POST(request: Request) {
  const session = await getSession()
  if (!session.accountId) return Response.json({ success: false, error: 'notAuthenticated' }, { status: 401 })
  let url = ''
  try {
    const b = await request.json()
    url = String(b.url ?? '')
  } catch {
    return Response.json({ success: false, error: 'invalidRequest' }, { status: 400 })
  }
  if (!url) return Response.json({ success: false, error: 'siteNotFound' })
  return Response.json(await registerVote(session.accountId, url))
}
