import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/db';

export const dynamic = 'force-dynamic';

/**
 * POST /api/analytics/errors/[id]/resolve
 * Mark an error as resolved
 */
export async function POST(
  request: NextRequest,
  { params }: { params: { id: string } }
) {
  try {
    const session = await getServerSession(authOptions);
    
    if (!session) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }

    const { id } = params;

    const errorLog = await prisma.errorLog.update({
      where: { id },
      data: {
        status: 'resolved',
        resolvedAt: new Date(),
        resolvedBy: session.user?.email || 'unknown',
      },
    });

    return NextResponse.json({
      success: true,
      errorLog,
    });
  } catch (error: any) {
    console.error('Error resolving error:', error);
    return NextResponse.json(
      { error: 'Failed to resolve error', details: error.message },
      { status: 500 }
    );
  }
}
