import { NextRequest, NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';
import { requireStaffAuth } from '@/lib/auth';

const prisma = new PrismaClient();

export async function POST(
    request: NextRequest,
    { params }: { params: Promise<{ id: string }> }
) {
    // Require curator authentication
    const { error, context } = await requireStaffAuth(request);
    if (error || !context) {
        return error!;
    }

    // Only curators can archive comments
    if (context.role !== 'curator') {
        return NextResponse.json(
            { error: 'Only curators can archive comments' },
            { status: 403 }
        );
    }

    try {
        const { id } = await params;
        const commentId = Number(id);
        const curatorId = context.curator.id;

        // Verify that this comment belongs to this curator
        const recipient = await prisma.curatorCommentRecipient.findUnique({
            where: {
                commentId_curatorId: {
                    commentId: commentId,
                    curatorId: curatorId
                }
            }
        });

        if (!recipient) {
            return NextResponse.json(
                { error: 'Comment not found or access denied' },
                { status: 404 }
            );
        }

        // Update archived flag
        await prisma.curatorCommentRecipient.update({
            where: {
                id: recipient.id
            },
            data: {
                archived: true
            }
        });

        return NextResponse.json({ success: true });
    } catch (error) {
        console.error('Error archiving comment:', error);
        return NextResponse.json(
            { error: 'Internal Server Error' },
            { status: 500 }
        );
    }
}

