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


// GET ALL DISCUSSION DIVISION ACTIVE = TRUE
export async function GET(request: Request) {
   try {
      const { searchParams } = new URL(request.url);
      const idDivision = searchParams.get("division");
      const search = searchParams.get('search');
      const page = searchParams.get('page');
      const status = searchParams.get('status');
      const isActive = searchParams.get('active');
      const villageId = searchParams.get('desa');
      const get = searchParams.get('get');

      let getFix = 0;
      if (get == null || get == undefined || get == "" || _.isNaN(Number(get))) {
         getFix = 10;
      } else {
         getFix = Number(get);
      }

      const dataSkip = page == null || page == undefined ? 0 : Number(page) * getFix - getFix;

      let kondisi: any = {
         isActive: isActive == "false" ? false : true,
         status: status == "close" ? 2 : 1,
         Division: {
            idVillage: String(villageId)
         },
         desc: {
            contains: (search == undefined || search == "null") ? "" : search,
            mode: "insensitive"
         },
      }


      if (idDivision != "null" && idDivision != null && idDivision != undefined) {
         kondisi = {
            isActive: isActive == "false" ? false : true,
            status: status == "close" ? 2 : 1,
            idDivision: idDivision,
            Division: {
               idVillage: String(villageId)
            },
            desc: {
               contains: (search == undefined || search == "null") ? "" : search,
               mode: "insensitive"
            },
         }
      }

      const data = await prisma.divisionDisscussion.findMany({
         skip: dataSkip,
         take: getFix,
         where: kondisi,
         orderBy: {
            createdAt: 'desc'
         },
         select: {
            id: true,
            desc: true,
            status: true,
            createdAt: true,
            idDivision: true,
            Division: {
               select: {
                  name: true,
               }
            },
            DivisionDisscussionComment: {
               select: {
                  id: true,
               }
            }
         }
      });

      const fixData = data.map((v: any) => ({
         ..._.omit(v, ["DivisionDisscussionComment", "status", "Division"]),
         totalKomentar: v.DivisionDisscussionComment.length,
         status: v.status == 1 ? "Open" : "Close",
         division: v.Division.name
      }))

      return NextResponse.json({ success: true, message: "Berhasil mendapatkan diskusi", data: fixData, }, { 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 });
   }
}