import { deleteSession } from '../../utils/auth.js'

export default defineEventHandler(async (event) => {
  try {
    const token = getCookie(event, 'auth_token')

    if (token) {
      await deleteSession(token)
    }

    // Delete cookie
    deleteCookie(event, 'auth_token')

    return {
      success: true,
      message: 'Erfolgreich abgemeldet'
    }
  } catch (error) {
    console.error('Logout-Fehler:', error)
    throw createError({
      statusCode: 500,
      message: 'Abmeldung fehlgeschlagen'
    })
  }
})

