import { NextResponse } from 'next/server';
import { getNotificationDispatchDetail } from '@/features/foundation/notifications/server/service';
import { PERMISSIONS } from '@/lib/auth/rbac';
import { AuthError, requireOrganizationAccess } from '@/lib/auth/session';

export async function GET(
  _request: Request,
  context: { params: Promise<{ id: string }> },
) {
  try {
    const { organization } = await requireOrganizationAccess({
      permission: PERMISSIONS.notificationViewHistory,
    });
    const { id } = await context.params;
    const result = await getNotificationDispatchDetail(organization.id, id);

    return NextResponse.json(result);
  } catch (error) {
    if (error instanceof AuthError) {
      return NextResponse.json({ message: error.message }, { status: error.status });
    }

    if (error instanceof Error) {
      return NextResponse.json({ message: error.message }, { status: 400 });
    }

    return NextResponse.json({ message: 'Unable to load notification detail' }, { status: 500 });
  }
}
