import { NextResponse } from 'next/server';
import { cookies } from 'next/headers';
import { getCurrentAdmin } from '@/lib/admin-auth';

export const runtime = 'nodejs';

export async function GET(request: Request) {
  try {
    console.log('Admin auth check - starting...');

    const cookieStore = await cookies();
    const token = cookieStore.get('adminToken')?.value;

    console.log('Admin token found:', !!token);

    if (!token) {
      console.log('No admin token found in cookies');
      return NextResponse.json(
        { error: 'Not authenticated - no token' },
        { status: 401 }
      );
    }

    const admin = await getCurrentAdmin(request as any);
    console.log('Admin user found:', !!admin);

    if (!admin) {
      console.log('Admin token invalid or user not found');
      return NextResponse.json(
        { error: 'Not authenticated - invalid token' },
        { status: 401 }
      );
    }

    return NextResponse.json({ user: admin });
  } catch (error) {
    console.error('Get admin user error:', error);
    return NextResponse.json(
      { error: 'Server error' },
      { status: 500 }
    );
  }
}