import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { canManageHumanCapital } from "@/lib/human-capital";
import { listHumanCapitalWorkspaceHistory } from "@/lib/human-capital/workspace";

export async function GET() {
  const session = await getServerSession(authOptions);
  if (!session?.user) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  if (!canManageHumanCapital(session.user.role, session.user.department)) {
    return NextResponse.json({ error: "Only owner/HC leaders can view history." }, { status: 403 });
  }

  const history = await listHumanCapitalWorkspaceHistory();
  return NextResponse.json({ ok: true, history });
}
