import { NextResponse } from 'next/server';
import { getUserPermissionAssignmentDetailForOrganization } from '@/features/permission-management/server/user-permission-data';
import { AuthError, requireSuperAdminOrganizationAccess } from '@/lib/auth/session';

export async function GET(
  _: Request,
  { params }: { params: Promise<{ userId: string }> }
) {
  try {
    const { organization } = await requireSuperAdminOrganizationAccess();
    const detail = await getUserPermissionAssignmentDetailForOrganization({
      organizationId: organization.id,
      userId: (await params).userId
    });

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

    if (error instanceof Error && error.message === 'NOT_FOUND') {
      return NextResponse.json({ message: 'User permission detail not found' }, { status: 404 });
    }

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