import { prisma } from "@/module/_global";
import { funGetUserById } from "@/module/auth";
import { NextResponse } from "next/server";

export async function POST(request: Request) {
   try {
      const { token, user } = (await request.json());

      const userMobile = await funGetUserById({ id: user })
      if (userMobile.id == "null" || userMobile.id == undefined || userMobile.id == "") {
         return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
      }

      const cek = await prisma.tokenDeviceUser.count({
         where: {
            idUser: userMobile.id,
            token
         }
      })

      if (cek > 0) {
         return NextResponse.json({ success: true, message: "Token terdaftar", data: true }, { status: 200 });
      } else {
         return NextResponse.json({ success: false, message: "Token tidak terdaftar", data: false }, { status: 200 })
      }


   } catch (error) {
      console.error(error);
      return NextResponse.json({ success: false, message: "Gagal mengecek token, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
   }
};