import { NextResponse } from 'next/server';
import { prisma } from '../../../../lib/prisma';
import { UserStatus } from '@prisma/client';
import { requireAuth } from '../../../../lib/jwt';

export async function GET(req: Request) {
  try {
    const session = await requireAuth(req);
    const user = await prisma.user.findUnique({ where: { id: session.userId }, select: { status: true } });
    if (!user || user.status !== UserStatus.ACTIVE) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }
    const listings = await prisma.listing.findMany({
      where: { ownerId: session.userId },
      select: { id: true, status: true, translations: { select: { slug: true, title: true, locale: true } } },
      orderBy: { createdAt: 'desc' },
    });
    return NextResponse.json({ listings });
  } catch (error) {
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }
}
