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

//GET ALL CALENDER
export async function GET(request: Request) {
   try {

      const { searchParams } = new URL(request.url);
      const idDivision = searchParams.get("division");
      const isDate = searchParams.get("date")
      const villageId = searchParams.get("desa")
      const active = searchParams.get("active")
      const search = searchParams.get("search")
      const page = searchParams.get("page")
      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 = {}

      if (idDivision != "" && idDivision != null && idDivision != undefined) {
         if (isDate != null && isDate != undefined && isDate != "") {
            kondisi = {
               idDivision: String(idDivision),
               dateStart: new Date(String(isDate)),
               DivisionCalendar: {
                  title: {
                     contains: (search == undefined || search == null) ? "" : search,
                     mode: "insensitive"
                  },
                  isActive: (active == "false" || active == undefined) ? false : true,
                  Division: {
                     idVillage: String(villageId)
                  }
               }
            }
         } else {
            kondisi = {
               idDivision: String(idDivision),
               DivisionCalendar: {
                  title: {
                     contains: (search == undefined || search == null) ? "" : search,
                     mode: "insensitive"
                  },
                  isActive: (active == "false" || active == undefined) ? false : true,
                  Division: {
                     idVillage: String(villageId)
                  }
               }
            }
         }
      } else {
         if (isDate != null && isDate != undefined && isDate != "") {
            kondisi = {
               dateStart: new Date(String(isDate)),
               DivisionCalendar: {
                  title: {
                     contains: (search == undefined || search == null) ? "" : search,
                     mode: "insensitive"
                  },
                  isActive: (active == "false" || active == undefined) ? false : true,
                  Division: {
                     idVillage: String(villageId)
                  }
               }
            }
         } else {
            kondisi = {
               DivisionCalendar: {
                  title: {
                     contains: (search == undefined || search == null) ? "" : search,
                     mode: "insensitive"
                  },
                  isActive: (active == "false" || active == undefined) ? false : true,
                  Division: {
                     idVillage: String(villageId)
                  }
               }
            }
         }
      }

      const data = await prisma.divisionCalendarReminder.findMany({
         where: kondisi,
         skip: dataSkip,
         take: getFix,
         select: {
            id: true,
            dateStart: true,
            timeStart: true,
            timeEnd: true,
            createdAt: true,
            DivisionCalendar: {
               select: {
                  isActive: true,
                  title: true,
                  desc: true,
                  User: {
                     select: {
                        name: true
                     }
                  }
               }
            }
         },
         orderBy: [
            {
               dateStart: 'asc'
            },
            {
               timeStart: 'asc'
            },
            {
               timeEnd: 'asc'
            }
         ]
      });

      const allOmit = data.map((v: any) => ({
         ..._.omit(v, ["DivisionCalendar", "User"]),
         title: v.DivisionCalendar.title,
         desc: v.DivisionCalendar.desc,
         createdBy: v.DivisionCalendar.User.name,
         isActive: v.DivisionCalendar.isActive,
         timeStart: moment.utc(v.timeStart).format('HH:mm'),
         timeEnd: moment.utc(v.timeEnd).format('HH:mm')
      }))


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

   } catch (error) {
      console.error(error)
      return NextResponse.json({ success: false, message: "Gagal mendapatkan kalender, data tidak ditemukan (error: 500)" }, { status: 404 });
   }
}