import { query, queryOne } from '../../../utils/db'
import { privnetScheduler } from '../../../utils/privnet-scheduler'

export default defineEventHandler(async () => {
  const status = await queryOne(`
    SELECT 
      ps.*,
      pt.name as last_target_name,
      pt.url as last_target_url
    FROM privnet_status ps
    LEFT JOIN privnet_targets pt ON ps.last_target_id = pt.id
    WHERE ps.id = 1
  `)
  
  const recentLogs = await query(`
    SELECT 
      pl.*,
      pt.name as target_name,
      pt.url as target_url
    FROM privnet_logs pl
    JOIN privnet_targets pt ON pl.target_id = pt.id
    ORDER BY pl.checked_at DESC
    LIMIT 10
  `)
  
  const targetCount = await queryOne<{ cnt: number }>(`
    SELECT COUNT(*) as cnt FROM privnet_targets WHERE is_active = 1
  `)

  return {
    status,
    recentLogs,
    targetCount: targetCount?.cnt || 0,
    schedulerRunning: privnetScheduler.getIsRunning()
  }
})
