import { NextResponse } from 'next/server'
import { prisma } from '@/lib/db'

export const runtime = 'nodejs'

export async function GET() {
  try {
    // Get table structure for User table
    const tableInfo = await prisma.$queryRaw`
      SELECT column_name, data_type, is_nullable, column_default
      FROM information_schema.columns
      WHERE table_name = 'User' AND table_schema = 'public'
      ORDER BY ordinal_position;
    `

    // Also try to get one user record to see what fields are actually there
    let sampleUser = null
    try {
      sampleUser = await prisma.$queryRaw`SELECT * FROM "User" LIMIT 1;`
    } catch (e) {
      sampleUser = { error: 'Could not fetch sample user: ' + ((e as any)?.message || e) }
    }

    return NextResponse.json({
      tableStructure: tableInfo,
      sampleUser: sampleUser
    })
  } catch (error) {
    console.error('Schema debug error:', error)
    return NextResponse.json({
      error: 'Schema debug failed',
      details: (error as any)?.message || error
    }, { status: 500 })
  }
}
