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


// GET ALL DATA TUGAS DIVISI
export async function GET(request: Request) {
   try {
      const { searchParams } = new URL(request.url);
      const villageId = searchParams.get('desa');
      const division = searchParams.get('division');
      const search = searchParams.get('search');
      const status = searchParams.get('status');
      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,
         Division: {
            idVillage: String(villageId)
         },
         title: {
            contains: (search == undefined || search == "null") ? "" : search,
            mode: "insensitive"
         }
      }

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

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


      const data = await prisma.divisionProject.findMany({
         skip: dataSkip,
         take: getFix,
         where: kondisi,
         select: {
            id: true,
            idDivision: true,
            title: true,
            desc: true,
            status: true,
            DivisionProjectTask: {
               where: {
                  isActive: true
               },
               select: {
                  title: true,
                  status: true
               }
            },
            DivisionProjectMember: {
               where: {
                  isActive: true
               },
               select: {
                  idUser: true
               }
            },
            Division: {
               select: {
                  name: true
               }
            }
         },
         orderBy: {
            createdAt: "desc"
         }
      });

      const formatData = data.map((v: any) => ({
         ..._.omit(v, ["DivisionProjectTask", "DivisionProjectMember", "status", "Division"]),
         division: v.Division.name,
         status: v.status == 1 ? "dikerjakan" : v.status == 2 ? "selesai" : v.status == 3 ? "batal" : "segera",
         progress: ceil((v.DivisionProjectTask.filter((i: any) => i.status == 1).length * 100) / v.DivisionProjectTask.length),
         member: v.DivisionProjectMember.length,
      }))

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

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