import { NextRequest, NextResponse } from 'next/server';
import { db } from '@/lib/db';
import { getCookieToken, getSessionUser } from '@/lib/auth';

export async function GET(req: NextRequest) {
  try {
    const token = getCookieToken(req);
    if (!token) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

    const user = await getSessionUser(token);
    if (!user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

    const logs = await db.activityLog.findMany({
      where: { userId: user.id },
      orderBy: { createdAt: 'desc' },
      take: 50,
    });

    return NextResponse.json({ activities: logs });
  } catch (error) {
    console.error('Activity fetch error:', error);
    return NextResponse.json({ error: 'Internal error' }, { status: 500 });
  }
}