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

export default defineEventHandler(async (event) => {
  const id = getRouterParam(event, 'id')
  const body = await readBody(event)
  
  const { name, url, is_active } = body
  
  if (!name || !url) {
    throw createError({
      statusCode: 400,
      message: 'name과 url은 필수입니다'
    })
  }
  
  await execute(`
    UPDATE privnet_targets 
    SET name = $1, url = $2, is_active = $3, updated_at = TO_CHAR(NOW(), 'YYYY-MM-DD HH24:MI:SS')
    WHERE id = $4
  `, [name, url, is_active ? 1 : 0, id])
  
  return {
    id: Number(id),
    name,
    url,
    is_active: is_active ? 1 : 0
  }
})
