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

export async function POST(req) {
    const { delta, entries, id, name, navigationType, rating, value } = await req.json();
    const { user } = await getUserFromSession();

    if (!user) {
        user = { USER_ID: null };
    }

    const headers = (await req).headers;

    const [rows] = await (await db).query(
        "INSERT INTO statistics (DELTA, ENTRIES, EVENT_ID, NAME, NAVIGATION_TYPE, RATING, VALUE, USER, PAGE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
        [delta, JSON.stringify(entries), id, name, navigationType, rating, value, user.USER_ID, headers.get("referer")],
    );

    return NextResponse.json({ success: true });
}