import { NextResponse } from "next/server";
import { setActiveAccountForUser } from "@/lib/account-context";
import { assertAccountOwnedByUser } from "@/lib/auth/accounts";
import { apiRouteErrorResponse } from "@/lib/auth/api";
import { requireSessionUser } from "@/lib/auth/session";

export async function POST(
  _request: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const user = await requireSessionUser();
    const { id } = await params;
    const account = await assertAccountOwnedByUser(user.id, id);
    await setActiveAccountForUser(user.id, account.id);

    return NextResponse.json({ success: true, account });
  } catch (error) {
    return apiRouteErrorResponse(error, "accounts/activate");
  }
}