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


// ADD MEMBER DISCUSSION GENERAL
export async function POST(request: Request, context: { params: { id: string } }) {
   try {
      const { id } = context.params
      const { member, 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.discussion.count({
         where: {
            id,
            isActive: true
         }
      })

      if (cek == 0) {
         return NextResponse.json({ success: false, message: "Gagal menambahkan anggota, data tidak ditemukan" }, { status: 200 });
      }

      if (member.length > 0) {
         const dataMember = member.map((v: any) => ({
            ..._.omit(v, ["idUser", "name", "img"]),
            idDiscussion: id,
            idUser: v.idUser
         }))

         const insertMember = await prisma.discussionMember.createMany({
            data: dataMember
         })
      }

      // create log user
      const log = await createLogUserMobile({ act: 'CREATE', desc: 'User menambah anggota diskusi umum', table: 'discussion', data: String(id), user: String(userMobile.id) })
      return NextResponse.json({ success: true, message: "Berhasil menambahkan anggota diskusi umum" }, { status: 200 });

   } catch (error) {
      console.error(error);
      return NextResponse.json({ success: false, message: "Gagal menambahkan anggota, coba lagi nanti (error : 500)" }, { status: 500 });
   }
}


// MENGELUARKAN ANGGOTA
export async function DELETE(request: Request, context: { params: { id: string } }) {
   try {
      const { id } = context.params
      const { idUser, user } = (await request.json());

      const userMobile = await funGetUserById({ id: String(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.discussion.count({
         where: {
            id,
            isActive: true
         }
      })

      if (cek == 0) {
         return NextResponse.json({ success: false, message: "Gagal, data tidak ditemukan" }, { status: 200 });
      }

      const deleteMember = await prisma.discussionMember.deleteMany({
         where: {
            idDiscussion: id,
            idUser
         }
      })

      // create log user
      const log = await createLogUserMobile({ act: 'DELETE', desc: 'User mengeluarkan anggota diskusi umum', table: 'discussion', data: String(id), user: userMobile.id })

      return NextResponse.json({ success: true, message: "Berhasil mengeluarkan anggota diskusi umum" }, { status: 200 });

   } catch (error) {
      console.error(error);
      return NextResponse.json({ success: false, message: "Gagal mengeluarkan anggota, coba lagi nanti (error : 500)" }, { status: 500 });
   }
}