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

export default defineEventHandler(async (event) => {
  const queryParams = getQuery(event)
  const targetId = queryParams.target_id as string
  
  if (!targetId) {
    throw createError({
      statusCode: 400,
      message: 'target_id is required'
    })
  }
  
  const snapshot = await queryOne(`
    SELECT 
      s.*,
      t.server_name,
      t.server_ip
    FROM server_snapshots s
    JOIN server_targets t ON s.target_id = t.target_id
    WHERE s.target_id = $1
    ORDER BY s.collected_at DESC
    LIMIT 1
  `, [targetId])
  
  return snapshot || null
})
