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


// GET ONE DETAIL DISKUSI UMUM
export async function GET(request: Request, context: { params: { id: string } }) {
   try {
      let dataFix
      const { id } = context.params

      const { searchParams } = new URL(request.url);
      const kategori = searchParams.get("cat");
      const idVillage = searchParams.get("desa");

      const cek = await prisma.discussion.count({
         where: {
            id,
            idVillage: String(idVillage)
         }
      })

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

      if (kategori == "comment") {
         const data = await prisma.discussionComment.findMany({
            where: {
               idDiscussion: id,
               isActive: true
            },
            select: {
               id: true,
               comment: true,
               createdAt: true,
               idUser: true,
               User: {
                  select: {
                     name: true,
                     img: true
                  }
               }
            }
         })

         dataFix = data.map((v: any) => ({
            ..._.omit(v, ["User",]),
            username: v.User.name,
            img: v.User.img
         }))

      } else if (kategori == "member") {
         const data = await prisma.discussionMember.findMany({
            where: {
               idDiscussion: id,
               isActive: true
            },
            select: {
               idUser: true,
               User: {
                  select: {
                     name: true,
                     img: true
                  }
               }
            }
         })

         dataFix = data.map((v: any) => ({
            ..._.omit(v, ["User",]),
            name: v.User.name,
            img: v.User.img
         }))
      } else {
         const data = await prisma.discussion.findUnique({
            where: {
               id,
               idVillage: String(idVillage)
            },
            select: {
               isActive: true,
               id: true,
               title: true,
               idGroup: true,
               desc: true,
               status: true,
               createdAt: true,
               Group: {
                  select: {
                     name: true,
                  }
               }
            }
         })

         dataFix = {
            id: data?.id,
            isActive: data?.isActive,
            idGroup: data?.idGroup,
            group: data?.Group.name,
            title: data?.title,
            desc: data?.desc,
            status: data?.status == 1 ? "Open" : "Close",
            createdAt: data?.createdAt
         }
      }


      return NextResponse.json({ success: true, message: "Berhasil mendapatkan diskusi", data: dataFix }, { status: 200 });


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