import { deleteCookie, getCookie } from 'h3'
import { getDb } from '../../utils/db.js'

export default defineEventHandler(async (event) => {
  const sid = getCookie(event, 'session_id')
  if (sid) {
    try {
      const { run } = await getDb()
      await run('DELETE FROM sessions WHERE id = ?', [sid])
    }
    catch {
      // ignore
    }
    deleteCookie(event, 'session_id', { path: '/' })
  }
  setResponseStatus(event, 204)
  return null
})
