import { queryOne } from '../../../../utils/db'

export default defineEventHandler(async (event) => {
  const body = await readBody(event)
  
  const { name, url, is_active } = body
  
  if (!name || !url) {
    throw createError({
      statusCode: 400,
      message: 'name과 url은 필수입니다'
    })
  }
  
  const result = await queryOne<{ id: number }>(`
    INSERT INTO pubnet_targets (name, url, is_active)
    VALUES ($1, $2, $3)
    RETURNING id
  `, [name, url, is_active ? 1 : 0])
  
  return {
    id: result?.id,
    name,
    url,
    is_active: is_active ? 1 : 0
  }
})
