/**
 * Onboarding Completion API
 * POST: Mark onboarding as complete
 */

import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/lib/auth";
import { prisma as db } from "@/lib/db";

// POST /api/onboarding/complete - Mark onboarding as complete
export async function POST(req: NextRequest) {
  try {
    const session = await auth();

    if (!session?.user?.id) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    const now = new Date();

    // Mark onboarding as complete
    const progress = await db.onboarding_progress.update({
      where: { userId: session.user.id },
      data: {
        completedAt: now,
        lastStepAt: now,
      },
    });

    return NextResponse.json({
      success: true,
      progress,
    });
  } catch (error) {
    console.error("Error completing onboarding:", error);
    return NextResponse.json(
      { error: "Internal server error" },
      { status: 500 },
    );
  }
}
