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

export default defineEventHandler(async (event) => {
  const queryParams = getQuery(event)
  const { year, month, day, hour } = queryParams as { 
    year?: string, month?: string, day?: string, hour?: string 
  }
  
  if (!year || !month || !day || !hour) {
    return { error: 'year, month, day, hour are required' }
  }
  
  const startTime = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')} ${hour.padStart(2, '0')}:00:00`
  const endTime = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')} ${hour.padStart(2, '0')}:59:59`
  
  const logs = await query(`
    SELECT 
      l.id,
      l.checked_at,
      l.is_success,
      t.name as target_name,
      t.url as target_url
    FROM pubnet_logs l
    JOIN pubnet_targets t ON l.target_id = t.id
    WHERE l.checked_at >= $1 AND l.checked_at <= $2
    ORDER BY l.checked_at DESC
  `, [startTime, endTime])

  return { logs }
})
