import { prisma } from "@/module/_global";
import { funGetUserByCookies } from "@/module/auth";
import { createLogUser } from "@/module/user";
import _ from "lodash";
import moment from "moment";
import { NextResponse } from "next/server";


// GET ALL DOCUMENT
export async function GET(request: Request) {
   try {
      const user = await funGetUserByCookies()
      if (user.id == undefined) {
         return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
      }


      const { searchParams } = new URL(request.url);
      const idDivision = searchParams.get("division");
      const path = searchParams.get("path");
      const category = searchParams.get("category");

      const cekDivision = await prisma.division.count({
         where: {
            id: String(idDivision),
            // isActive: true
         }
      })

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

      let statusAkses = false
      let aksesPath = String(path)
      if (path != "home" && path != "null" && path != "undefined" && path != "") {
         const cekPath = await prisma.divisionDocumentFolderFile.count({
            where: {
               isActive: true,
               id: String(path),
               idDivision: String(idDivision)
            }
         })

         const cekSharePath = await prisma.divisionDocumentShare.count({
            where: {
               isActive: true,
               idDivision: String(idDivision),
               idDocument: String(path),
               DivisionDocumentFolderFile: {
                  isActive: true
               }
            }
         })

         if (cekPath == 0 && cekSharePath == 0) {
            do {
               const dataPath = await prisma.divisionDocumentFolderFile.findUnique({
                  where: {
                     id: String(aksesPath)
                  }
               })

               if (dataPath) {
                  const cekShare = await prisma.divisionDocumentShare.count({
                     where: {
                        isActive: true,
                        idDivision: String(idDivision),
                        idDocument: String(aksesPath)
                     }
                  })
                  if (cekShare == 0) {
                     statusAkses = false
                     aksesPath = dataPath.path
                  } else {
                     statusAkses = true
                  }

               } else {
                  aksesPath = "home"
               }
            } while (aksesPath != "home" && statusAkses == false);

            if (statusAkses == false) {
               return NextResponse.json({ success: false, message: "Data tidak ditemukan / tidak memilik hak akses" }, { status: 404 });
            }
         }
      }


      let kondisi: any = {
         isActive: true,
         idDivision: String(idDivision),
         path: (path == "undefined" || path == "null" || path == "" || path == null) ? "home" : path
      }

      let formatDataShare: any[] = [];

      if (category == "folder") {
         kondisi = {
            isActive: true,
            idDivision: String(idDivision),
            path: (path == "undefined" || path == "null" || path == "" || path == null) ? "home" : path,
            category: "FOLDER"
         }
      } else {
         if (path == "home" || path == "null" || path == "undefined") {
            const dataShare = await prisma.divisionDocumentShare.findMany({
               where: {
                  isActive: true,
                  idDivision: String(idDivision),
                  DivisionDocumentFolderFile: {
                     isActive: true
                  }
               },
               select: {
                  DivisionDocumentFolderFile: {
                     select: {
                        idStorage: true,
                        id: true,
                        category: true,
                        name: true,
                        extension: true,
                        path: true,
                        User: {
                           select: {
                              name: true
                           }
                        },
                        createdAt: true,
                        updatedAt: true
                     }
                  }
               },
               orderBy: {
                  DivisionDocumentFolderFile: {
                     createdAt: 'desc'
                  }
               }
            })

            formatDataShare = dataShare.map((v: any) => ({
               ..._.omit(v, ["DivisionDocumentFolderFile"]),
               idStorage: v.DivisionDocumentFolderFile.idStorage,
               id: v.DivisionDocumentFolderFile.id,
               category: v.DivisionDocumentFolderFile.category,
               name: v.DivisionDocumentFolderFile.name,
               extension: v.DivisionDocumentFolderFile.extension,
               path: v.DivisionDocumentFolderFile.path,
               createdBy: v.DivisionDocumentFolderFile.User.name,
               createdAt: v.DivisionDocumentFolderFile.createdAt,
               updatedAt: v.DivisionDocumentFolderFile.updatedAt,
               share: true
            }))

         } else {
            kondisi = {
               isActive: true,
               path: (path == "undefined" || path == "null" || path == null) ? "home" : path
            }
         }
      }


      const data = await prisma.divisionDocumentFolderFile.findMany({
         where: kondisi,
         select: {
            id: true,
            category: true,
            name: true,
            extension: true,
            idStorage: true,
            path: true,
            User: {
               select: {
                  name: true
               }
            },
            createdAt: true,
            updatedAt: true
         },
         orderBy: {
            createdAt: 'desc'
         }
      })

      const allData = data.map((v: any) => ({
         ..._.omit(v, ["User", "createdAt", "updatedAt"]),
         createdBy: v.User.name,
         createdAt: v.createdAt,
         updatedAt: v.updatedAt,
         share: false
      }))

      if (formatDataShare.length > 0) {
         allData.push(...formatDataShare)
      }

      const formatData = _.orderBy(allData, ['category', 'createdAt'], ['desc', 'desc']);

      const fixData = formatData.map((v: any) => ({
         ..._.omit(v, ["createdAt", "updatedAt"]),
         createdAt: moment(v.createdAt).format("DD-MM-YYYY HH:mm"),
         updatedAt: moment(v.updatedAt).format("DD-MM-YYYY HH:mm"),
      }))

      let pathNow = path
      let jalur = []

      if (path != "home" && path != "null" && path != "undefined" && path != "") {
         do {
            const dataPath = await prisma.divisionDocumentFolderFile.findUnique({
               where: {
                  id: String(pathNow)
               }
            })

            if (dataPath && (dataPath.idDivision == idDivision || path == pathNow || pathNow == aksesPath)) {
               const fix = {
                  id: String(pathNow),
                  name: dataPath.name,
               }
               jalur.push(fix)
               pathNow = dataPath.path

            } else {
               pathNow = "home"
            }
         } while (pathNow != "home");

      }

      jalur.push({ id: 'home', name: 'home' })
      jalur = [...jalur].reverse()

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

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



// CREATE FOLDER
export async function POST(request: Request) {
   try {
      const user = await funGetUserByCookies()
      if (user.id == undefined) {
         return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
      }

      const { name, path, idDivision } = (await request.json());

      const cekDivision = await prisma.division.count({
         where: {
            id: String(idDivision),
            isActive: true
         }
      })

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

      if (path != "home") {
         const cekPath = await prisma.divisionDocumentFolderFile.count({
            where: {
               isActive: true,
               id: path
            }
         })

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

      const nameFile = await prisma.divisionDocumentFolderFile.count({
         where: {
            name,
            idDivision,
            path,
            extension: "folder",
            category: "FOLDER",
            isActive: true
         }
      })


      if (nameFile > 0) {
         return NextResponse.json({ success: false, message: "Gagal membuat folder baru, folder sudah ada" }, { status: 400 });
      }

      const data = await prisma.divisionDocumentFolderFile.create({
         data: {
            name,
            path,
            idDivision,
            category: "FOLDER",
            extension: "folder",
            createdBy: user.id,
         },
         select: {
            id: true
         }
      });

      // create log user
      const log = await createLogUser({ act: 'CREATE', desc: 'User membuat folder baru', table: 'divisionDocumentFolderFile', data: data.id })

      return NextResponse.json({ success: true, message: "Berhasil membuat folder baru" }, { status: 200 });
   } catch (error) {
      console.error(error);
      return NextResponse.json({ success: false, message: "Gagal membuat folder, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
   }
};


// RENAME ITEM
export async function PUT(request: Request) {
   try {
      const user = await funGetUserByCookies()
      if (user.id == undefined) {
         return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
      }

      const { name, id, path, idDivision, extension } = (await request.json());
      const cekFile = await prisma.divisionDocumentFolderFile.count({
         where: {
            id: id,
            isActive: true
         }
      })

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

      const nameFile = await prisma.divisionDocumentFolderFile.count({
         where: {
            name,
            idDivision,
            path,
            extension,
            isActive: true,
            NOT: {
               id: id
            },
         }
      })


      if (nameFile > 0) {
         return NextResponse.json({ success: false, message: "Gagal mengubah nama item, item sudah ada" }, { status: 400 });
      }

      const update = await prisma.divisionDocumentFolderFile.update({
         where: {
            id: id
         },
         data: {
            name,
         }
      })


      // create log user
      const log = await createLogUser({ act: 'UPDATE', desc: 'User mengubah nama file atau folder', table: 'divisionDocumentFolderFile', data: id })


      return NextResponse.json({ success: true, message: "Berhasil mengubah nama item" }, { status: 200 });
   } catch (error) {
      console.error(error);
      return NextResponse.json({ success: false, message: "Gagal mengubah nama item (error: 500), coba lagi nanti", reason: (error as Error).message, }, { status: 500 });
   }
};


// DELETE ITEM
export async function DELETE(request: Request) {
   try {
      const user = await funGetUserByCookies()
      if (user.id == undefined) {
         return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 401 });
      }

      const data = await request.json()

      for (let i = 0; i < data.length; i++) {
         const id = data[i].id;
         const cekFile = await prisma.divisionDocumentFolderFile.update({
            where: {
               id: id
            },
            data: {
               isActive: false
            }
         })
      }

      // create log user
      const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus file atau folder', table: 'divisionDocumentFolderFile', data: '' })


      return NextResponse.json({ success: true, message: "Berhasil menghapus item" }, { status: 200 });
   } catch (error) {
      console.error(error);
      return NextResponse.json({ success: false, message: "Gagal menghapus item, coba lagi nanti (error: 500)", reason: (error as Error).message, }, { status: 500 });
   }
};