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


// GET ALL DATA PROJECT
export async function GET(request: Request) {
   try {
      const { searchParams } = new URL(request.url);
      const idVillage = searchParams.get('desa');
      const name = searchParams.get('search');
      const status = searchParams.get('status');
      const idGroup = searchParams.get("group");
      const page = searchParams.get('page');
      const get = searchParams.get('get');

      let getFix = 10;
      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: true,
         idVillage: String(idVillage),
         title: {
            contains: (name == undefined || name == "null") ? "" : name,
            mode: "insensitive"
         },
      }

      if (status != "null" && status != undefined && status != "") {
         kondisi = {
            ...kondisi,
            status: status == "segera" ? 0 : status == "dikerjakan" ? 1 : status == "selesai" ? 2 : status == "batal" ? 3 : 0
         }
      }


      if (idGroup != "null" && idGroup != undefined && idGroup != "") {
         kondisi = {
            ...kondisi,
            idGroup: String(idGroup)
         }
      }


      const data = await prisma.project.findMany({
         skip: dataSkip,
         take: getFix,
         where: kondisi,
         select: {
            id: true,
            idGroup: true,
            title: true,
            desc: true,
            status: true,
            ProjectMember: {
               where: {
                  isActive: true
               },
               select: {
                  idUser: true
               }
            },
            ProjectTask: {
               where: {
                  isActive: true
               },
               select: {
                  title: true,
                  status: true
               }
            },
            Group: {
               select: {
                  name: true
               }
            }
         },
         orderBy: {
            createdAt: 'desc'
         }
      })

      const omitData = data.map((v: any) => ({
         ..._.omit(v, ["ProjectMember", "ProjectTask", "status", "Group"]),
         group: v.Group.name,
         status: v.status == 1 ? "dikerjakan" : v.status == 2 ? "selesai" : v.status == 3 ? "batal" : "segera",
         progress: ceil((v.ProjectTask.filter((i: any) => i.status == 1).length * 100) / v.ProjectTask.length),
         member: v.ProjectMember.length
      }))


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

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