export const runtime = 'nodejs'
import { NextResponse } from 'next/server';
import { auth } from '@/auth';
import { PrismaClient } from '@/app/generated/prisma';

export async function GET() {
  const session = await auth();
  if (!session?.user?.email) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }
  const email = session.user.email as string;

  const prisma = new PrismaClient();
  try {
    let rows;
    if (email === 'wsx204@naver.com') {
      // 관리자: 전체 핸들
      rows = await prisma.handle.findMany({
        orderBy: { handle: 'asc' },
        select: { id: true, handle: true, avatar: true },
      });
    } else {
      // 일반 사용자: 자신의 핸들만
      rows = await prisma.handle.findMany({
        where: { users: { some: { email } } },
        orderBy: { handle: 'asc' },
        select: { id: true, handle: true, avatar: true },
      });
    }
    const items = rows.map(h => ({
      id: h.id,
      handle: h.handle,
      createtime: new Date().toISOString(),
      is_approved: false,
      icon: h.avatar,
    }));
    return NextResponse.json({ items });
  } catch (e) {
    console.error('list_channel 오류:', e);
    return NextResponse.json({ error: '조회 실패' }, { status: 500 });
  } finally {
    await prisma.$disconnect();
  }
}


