import { NextResponse } from 'next/server'
import pool from '@/lib/db'
import { getSessionFromCookie, validateSession } from '@/lib/auth'

export async function GET() {
  const session = await getSessionFromCookie()
  if (!session) {
    return NextResponse.json({ error: 'Not authenticated' }, { status: 401 })
  }

  const valid = await validateSession(session.sessionId, session.sub)
  if (!valid) {
    return NextResponse.json({ error: 'Session expired' }, { status: 401 })
  }

  const { rows } = await pool.query(
    `SELECT id, email, name, company, status, last_login_at, login_count, created_at, is_showcase, preferred_lang
     FROM pitch_investors WHERE id = $1`,
    [session.sub]
  )

  if (rows.length === 0 || rows[0].status === 'revoked') {
    return NextResponse.json({ error: 'Access revoked' }, { status: 403 })
  }

  return NextResponse.json({ investor: rows[0] })
}
