import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import { sharePointScannerService } from "@/lib/services/sharePointScannerService";

export async function GET(_: Request, { params }: { params: Promise<{ id: string }> }) {
  try {
    const auth = await getServerSession(authOptions);
    if (!auth?.user) return NextResponse.json({ error: "No autenticado" }, { status: 401 });
    const { id } = await params;
    return NextResponse.json({ success: true, session: await sharePointScannerService.getSession(id, auth.user.id, auth.user.role) });
  } catch (error) {
    const message = error instanceof Error ? error.message : "No se pudo obtener la sesión.";
    return NextResponse.json({ error: message }, { status: message.includes("permisos") ? 403 : 404 });
  }
}
