import { NextRequest, NextResponse } from 'next/server'
import { verifyUser } from '@/lib/auth'
import { pool } from '@/lib/db'

export async function GET(req: NextRequest){
  const token = req.cookies.get('iyjm_token')?.value
  if(!token) return NextResponse.json({ logged: false }, { status: 401 })
  const user = verifyUser(token)
  if(!user) return NextResponse.json({ logged: false }, { status: 401 })

  const [rows]: any = await pool.query('SELECT level FROM users WHERE id=?', [user.id])
  const level = rows[0]?.level || user.level

  return NextResponse.json({ logged: true, user: { ...user, level } })
}
