import { NextResponse } from "next/server";
import { db } from "@/lib/db";
import { auth } from "@/lib/auth";

// POST /api/progress — mark a lesson as complete
export async function POST(req: Request) {
  try {
    const session = await auth();
    if (!session?.user?.id) {
      return NextResponse.json({ success: false, error: "Neautorizat" }, { status: 401 });
    }

    const { lessonId, isCompleted = true, watchedSecs = 0 } = await req.json();
    if (!lessonId) {
      return NextResponse.json({ success: false, error: "lessonId lipsa." }, { status: 400 });
    }

    const progress = await db.lessonProgress.upsert({
      where: { userId_lessonId: { userId: session.user.id, lessonId } },
      update: { isCompleted, watchedSecs },
      create: { userId: session.user.id, lessonId, isCompleted, watchedSecs },
    });

    return NextResponse.json({ success: true, data: progress });
  } catch (error) {
    console.error("[/api/progress POST]", error);
    return NextResponse.json({ success: false, error: "Eroare server." }, { status: 500 });
  }
}
