import { NextResponse } from 'next/server';
import { getOrgRole } from '../../../../lib/org.js';
import { acknowledgeAlert, deleteAlert } from '../../../../lib/drift.js';

function requireAdmin(request) {
  if (getOrgRole(request) !== 'admin') {
    return NextResponse.json({ error: 'Admin access required' }, { status: 403 });
  }
  return null;
}

export async function PATCH(request, { params }) {
  const gate = requireAdmin(request);
  if (gate) return gate;
  try {
    const { alertId } = await params;
    const updated = await acknowledgeAlert(request, alertId);
    if (!updated) return NextResponse.json({ error: 'Alert not found' }, { status: 404 });
    return NextResponse.json(updated);
  } catch (err) {
    console.error('[drift/alerts/detail] PATCH error:', err);
    return NextResponse.json({ error: 'Failed to acknowledge alert' }, { status: 500 });
  }
}

export async function DELETE(request, { params }) {
  const gate = requireAdmin(request);
  if (gate) return gate;
  try {
    const { alertId } = await params;
    await deleteAlert(request, alertId);
    return NextResponse.json({ deleted: true });
  } catch (err) {
    console.error('[drift/alerts/detail] DELETE error:', err);
    return NextResponse.json({ error: 'Failed to delete alert' }, { status: 500 });
  }
}
