import { DIR, funUploadFile, prisma } from "@/module/_global";
import { funGetUserById } from "@/module/auth";
import { createLogUserMobile } from "@/module/user";
import _ from "lodash";
import { NextResponse } from "next/server";
import sharp from "sharp";

// GET ALL MEMBER / USER
export async function GET(request: Request) {
  try {
    let fixGroup
    const { searchParams } = new URL(request.url);
    const name = searchParams.get('search')
    const idGroup = searchParams.get("group");
    const active = searchParams.get("active");
    const page = searchParams.get('page');
    const userMobile = searchParams.get("user")
    const dataSkip = Number(page) * 10 - 10;

    if (userMobile == "null" || userMobile == undefined || userMobile == "") {
      return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
    }

    const user = await funGetUserById({ id: userMobile })

    if (idGroup == "null" || idGroup == undefined || idGroup == "" || idGroup == "undefined") {
      fixGroup = user.idGroup
    } else {
      fixGroup = idGroup
    }

    const filter = await prisma.group.findUnique({
      where: {
        id: fixGroup
      },
      select: {
        id: true,
        name: true
      }
    })

    if (page != "undefined" && page != "null" && page != "" && page != undefined && page != null) {
      const users = await prisma.user.findMany({
        skip: dataSkip,
        take: 10,
        where: {
          isActive: active == 'false' ? false : true,
          idGroup: String(fixGroup),
          name: {
            contains: (name == undefined || name == null) ? "" : name,
            mode: "insensitive",
          },
          NOT: {
            idUserRole: 'developer'
          }
        },
        select: {
          id: true,
          idUserRole: true,
          isActive: true,
          nik: true,
          name: true,
          phone: true,
          email: true,
          gender: true,
          img: true,
          Position: {
            select: {
              name: true,
            },
          },
          Group: {
            select: {
              name: true,
            },
          },
        },
        orderBy: {
          name: 'asc'
        }
      });

      const allData = users.map((v: any) => ({
        ..._.omit(v, ["Group", "Position"]),
        group: v.Group.name,
        position: v?.Position?.name
      }))

      return NextResponse.json({ success: true, message: "Berhasil member", data: allData, filter }, { status: 200 });
    } else {
      const users = await prisma.user.findMany({
        where: {
          isActive: active == 'false' ? false : true,
          idGroup: String(fixGroup),
          name: {
            contains: (name == undefined || name == null) ? "" : name,
            mode: "insensitive",
          },
          NOT: {
            idUserRole: 'developer'
          }
        },
        select: {
          id: true,
          idUserRole: true,
          isActive: true,
          nik: true,
          name: true,
          phone: true,
          email: true,
          gender: true,
          img: true,
          Position: {
            select: {
              name: true,
            },
          },
          Group: {
            select: {
              name: true,
            },
          },
        },
        orderBy: {
          name: 'asc'
        }
      });

      const allData = users.map((v: any) => ({
        ..._.omit(v, ["Group", "Position"]),
        group: v.Group.name,
        position: v?.Position?.name
      }))

      return NextResponse.json({ success: true, message: "Berhasil member", data: allData, filter }, { status: 200 });
    }


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


// CREATE MEMBER / USER
export async function POST(request: Request) {
  try {
    const body = await request.formData()
    const data = JSON.parse(body.get("data") as string)
    const file = body.get("file") as File
    const user = await funGetUserById({ id: data.user })

    if (user.id == "null" || user.id == undefined || user.id == "") {
      return NextResponse.json({ success: false, message: "Anda harus login untuk mengakses ini" }, { status: 200 });
    }

    const village = String(user.idVillage)
    let groupFix = data.idGroup

    if (groupFix == null || groupFix == undefined || groupFix == "") {
      groupFix = user.idGroup
    }

    const cekNIK = await prisma.user.count({
      where: {
        nik: data.nik
      },
    });

    const cekEmail = await prisma.user.count({
      where: {
        email: data.email
      },
    });

    const cekPhone = await prisma.user.count({
      where: {
        phone: "62" + data.phone
      },
    });


    if (cekNIK == 0 && cekEmail == 0 && cekPhone == 0) {
      const users = await prisma.user.create({
        data: {
          nik: data.nik,
          name: data.name,
          phone: "62" + data.phone,
          email: data.email,
          gender: data.gender,
          idGroup: groupFix,
          idVillage: village,
          idPosition: data.idPosition,
          idUserRole: data.idUserRole,
        },
        select: {
          id: true,
          idGroup: true,
        },
      });

      if (String(file) != "undefined" && String(file) != "null") {

        const fExt = file.name.split(".").pop()
        const fileName = user.id + '.' + fExt;

        // Resize ukuran
        const imageBuffer = await file.arrayBuffer();
        const resize = await sharp(imageBuffer).resize(300).toBuffer();

        // Convert buffer ke Blob
        const blob = new Blob([resize as any], { type: file.type });

        // Convert Blob ke File
        const resizedFile = new File([blob], fileName, {
          type: file.type,
          lastModified: new Date().getTime(),
        });

        // const newFile = new File([file], fileName, { type: file.type });

        const upload = await funUploadFile({ file: resizedFile, dirId: DIR.user })
        if (upload.success) {
          await prisma.user.update({
            where: {
              id: users.id
            },
            data: {
              img: upload.data.id
            }
          })
        }
      }

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

      return Response.json({ success: true, message: 'Sukses membuat user' }, { status: 200 });
    } else {
      if (cekNIK > 0) {
        return Response.json({ success: false, message: "NIK sudah terdaftar" }, { status: 200 });
      }
      
      if (cekEmail > 0) {
        return Response.json({ success: false, message: "Email sudah terdaftar" }, { status: 200 });
      }

      if (cekPhone > 0) {
        return Response.json({ success: false, message: "Nomor telepon sudah terdaftar" }, { status: 200 });
      }
    }

  } catch (error) {
    console.error(error);
    return Response.json({ success: false, message: "Gagal membuat anggota, coba lagi nanti (error: 500)" }, { status: 500 });
  }
}