import { DIR, funSendWebPush, funUploadFile, prisma } from "@/module/_global";
import { funGetUserById } from "@/module/auth";
import { createLogUserMobile } from "@/module/user";
import _, { ceil } from "lodash";
import moment from "moment";
import "moment/locale/id";
import { NextResponse } from "next/server";
import { sendFCMNotificationMany } from "../../../../../xsendMany";


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

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

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

        let grup, tahunFilter = String(tahun)
        const dataSkip = Number(page) * 10 - 10;
        const roleUser = userMobile.idUserRole
        const villageId = userMobile.idVillage
        const userId = userMobile.id

        if (idGroup == "null" || idGroup == undefined || idGroup == "" || idGroup == "undefined") {
            grup = userMobile.idGroup
        } else {
            grup = idGroup
        }

        if (tahun == "null" || tahun == undefined || tahun == "" || tahun == "undefined") {
            tahunFilter = new Date().getFullYear().toString();
        }

        const startTahun = new Date(`${tahunFilter}-01-01T00:00:00.000Z`);
        const endTahun = new Date(`${parseInt(tahunFilter) + 1}-01-01T00:00:00.000Z`);


        const cek = await prisma.group.count({
            where: {
                id: grup,
                isActive: true
            }
        })

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


        // JIKA (ROLE BUKAN USER DAN COADMIN) ATAU SEMUA ROLE DG KATEGORI == SEMUA
        let kondisi: any = {
            isActive: true,
            idVillage: String(villageId),
            idGroup: grup,
            title: {
                contains: (name == undefined || name == "null") ? "" : name,
                mode: "insensitive"
            },
            status: (status == "0" || status == "1" || status == "2" || status == "3") ? Number(status) : 0,
            createdAt: {
                gte: startTahun,
                lt: endTahun
            }
        }


        // JIKA ROLE = USER ATAU COADMIN DAN KATEGORI != SEMUA (KEGIATAN SAYA)
        if (roleUser != "supadmin" && roleUser != "developer" && roleUser != "cosupadmin" && roleUser != "admin") {
            if (kategori != "semua") {
                kondisi = {
                    isActive: true,
                    idVillage: String(villageId),
                    idGroup: grup,
                    title: {
                        contains: (name == undefined || name == "null") ? "" : name,
                        mode: "insensitive"
                    },
                    status: (status == "0" || status == "1" || status == "2" || status == "3") ? Number(status) : 0,
                    ProjectMember: {
                        some: {
                            idUser: String(userId)
                        }
                    },
                    createdAt: {
                        gte: startTahun,
                        lt: endTahun
                    }
                }
            }
        }


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

        const omitData = data.map((v: any) => ({
            ..._.omit(v, ["ProjectMember", "ProjectTask", "createdAt"]),
            progress: ceil((v.ProjectTask.filter((i: any) => i.status == 1).length * 100) / v.ProjectTask.length),
            member: v.ProjectMember.length,
            createdAt: moment(v.createdAt).format("LL")
        }))

        const totalData = await prisma.project.count({
            where: kondisi
        })

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


        return NextResponse.json({ success: true, message: "Berhasil mendapatkan kegiatan", data: omitData, filter, tahun: tahunFilter, total: totalData }, { 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 });
    }
}

// CREATE PROJECT 
export async function POST(request: Request) {
    try {
        const body = await request.formData()
        const dataBody = body.get("data")
        const cekFile = body.has("file0")

        const { idGroup, title, task, member, user } = JSON.parse(dataBody as string)

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


        const userId = userMobile.id
        const userRoleLogin = userMobile.idUserRole

        const data = await prisma.project.create({
            data: {
                idVillage: String(userMobile.idVillage),
                idGroup: String(idGroup),
                title,
                createdBy: String(userId)
            },
            select: {
                id: true
            }
        })

        if (task.length > 0) {
            const dataProject = task.map((v: any) => ({
                ..._.omit(v, ["dateStart", "dateEnd", "name", "dateEndFix", "dateStartFix", "dataDetail"]),
                idProject: data.id,
                title: v.title,
                dateStart: new Date(v.dateStartFix),
                dateEnd: new Date(v.dateEndFix),
            }))

            let dataDetailFix = []
            for (let i = 0; i < dataProject.length; i++) {
                const insertTask = await prisma.projectTask.create({
                    data: dataProject[i],
                    select: {
                        id: true
                    }
                })
                const dataDetail = task[i].dataDetail.map((v: any) => ({
                    ...v,
                    idTask: insertTask.id,
                    date: new Date(v.date),
                    timeStart: v.timeStart == null ? null : new Date(new Date('1970-01-01 ' + v.timeStart).getTime() - (new Date('1970-01-01 ' + v.timeStart).getTimezoneOffset() * 60000)).toISOString(),
                    timeEnd: v.timeEnd == null ? null : new Date(new Date('1970-01-01 ' + v.timeEnd).getTime() - (new Date('1970-01-01 ' + v.timeEnd).getTimezoneOffset() * 60000)).toISOString(),
                }))
                dataDetailFix.push(...dataDetail)
            }

            const insertDetail = await prisma.projectTaskDetail.createMany({
                data: dataDetailFix
            })
        }

        if (member.length > 0) {
            const dataMember = member.map((v: any) => ({
                ..._.omit(v, ["idUser", "name", "img"]),
                idProject: data.id,
                idUser: v.idUser,
            }))

            const insertMember = await prisma.projectMember.createMany({
                data: dataMember
            })
        }

        if (cekFile) {
            body.delete("data")
            for (var pair of body.entries()) {
                if (String(pair[0]).substring(0, 4) == "file") {
                    const file = body.get(pair[0]) as File
                    const fExt = file.name.split(".").pop()
                    const fName = decodeURIComponent(file.name.replace("." + fExt, ""))
                    const upload = await funUploadFile({ file: file, dirId: DIR.project })
                    if (upload.success) {
                        await prisma.projectFile.create({
                            data: {
                                idStorage: upload.data.id,
                                idProject: data.id,
                                name: fName,
                                extension: String(fExt)
                            }
                        })
                    }
                }
            }
        }

        const memberNotif = await prisma.projectMember.findMany({
            where: {
                idProject: data.id
            },
            select: {
                idUser: true,
                User: {
                    select: {
                        Subscribe: {
                            select: {
                                subscription: true
                            }
                        },
                        TokenDeviceUser: {
                            select: {
                                token: true
                            }
                        }
                    }
                }
            }
        })


        // mengirim notifikasi
        // dataFCM untuk push notifikasi mobile
        // datanotif untuk realtime notifikasi
        // datapush untuk web push notifikasi ketika aplikasi tidak 
        const dataFCM = memberNotif.map((v: any) => ({
            ..._.omit(v, ["idUser", "User", "Subscribe", "TokenDeviceUser"]),
            tokens: v.User.TokenDeviceUser.map((v: any) => v.token)
        }))
        const tokenDup = dataFCM.filter((v: any) => v.tokens.length > 0).map((v: any) => v.tokens).flat();

        const dataNotif = memberNotif.map((v: any) => ({
            ..._.omit(v, ["idUser", "User", "Subscribe", "TokenDeviceUser"]),
            idUserTo: v.idUser,
            idUserFrom: userId,
            category: 'project',
            idContent: data.id,
            title: 'Kegiatan Baru',
            desc: title
        }))

        const dataPush = memberNotif.map((v: any) => ({
            ..._.omit(v, ["idUser", "User", "Subscribe", "TokenDeviceUser"]),
            idUser: v.idUser,
            subscription: v.User.Subscribe?.subscription,
        }))

        if (userRoleLogin != "supadmin") {
            const perbekel = await prisma.user.findFirst({
                where: {
                    isActive: true,
                    idUserRole: "supadmin",
                    idVillage: userMobile.idVillage
                },
                select: {
                    id: true,
                    Subscribe: {
                        select: {
                            subscription: true
                        }
                    },
                    TokenDeviceUser: {
                        select: {
                            token: true
                        }
                    }
                }
            })

            tokenDup.push(perbekel?.TokenDeviceUser.map((v: any) => v.token).flat())

            dataNotif.push({
                idUserTo: perbekel?.id,
                idUserFrom: userId,
                category: 'project',
                idContent: data.id,
                title: 'Kegiatan Baru',
                desc: title
            })

            dataPush.push({
                idUser: perbekel?.id,
                subscription: perbekel?.Subscribe?.subscription
            })
        } else {
            const atasanGroup = await prisma.user.findMany({
                where: {
                    isActive: true,
                    idGroup: idGroup,
                    AND: {
                        OR: [
                            { idUserRole: 'cosupadmin' },
                            { idUserRole: 'admin' },
                        ]
                    }
                },
                select: {
                    id: true,
                    Subscribe: {
                        select: {
                            subscription: true
                        }
                    },
                    TokenDeviceUser: {
                        select: {
                            token: true
                        }
                    }
                }
            })

            const omitFCM = atasanGroup.map((v: any) => ({
                ..._.omit(v, ["id", "Subscribe", "TokenDeviceUser"]),
                tokens: v.TokenDeviceUser.map((v: any) => v.token)
            }))


            const omitData = atasanGroup.map((v: any) => ({
                ..._.omit(v, ["id", "Subscribe", "TokenDeviceUser"]),
                idUserTo: v.id,
                idUserFrom: userId,
                category: 'project',
                idContent: data.id,
                title: 'Kegiatan Baru',
                desc: title
            }))

            const omitPush = atasanGroup.map((v: any) => ({
                ..._.omit(v, ["id", "Subscribe", "TokenDeviceUser"]),
                idUser: v.id,
                subscription: v.Subscribe?.subscription,
            }))

            dataNotif.push(...omitData)
            dataPush.push(...omitPush)
            tokenDup.push(...omitFCM.map((v: any) => v.tokens).flat())
        }

        const dataNotifFilter = dataNotif.filter((item) => item.idUserTo != undefined && item.idUserTo != null && item.idUserTo != "" && item.idUserTo != userId)
        const dataNotifFilterUnique = dataNotifFilter
            .filter((v: any, index: number, self: any[]) =>
                index === self.findIndex((t: any) => t.idUserTo == v.idUserTo)
            )
        const pushNotif = dataPush.filter((item) => item.subscription != undefined)

        const sendWebPush = await funSendWebPush({ sub: pushNotif, message: { title: 'Kegiatan Baru', body: title } })
        const insertNotif = await prisma.notifications.createMany({
            data: dataNotifFilterUnique
        })

        const tokenUnique = [...new Set(tokenDup.flat())].filter((v: any) => v != undefined && v != null && v != "");
        await sendFCMNotificationMany({
            token: tokenUnique,
            title: "Kegiatan Baru",
            body: title,
            data: { id: data.id, category: "project", content: data.id }
        })


        // create log user
        const log = await createLogUserMobile({ act: 'CREATE', desc: 'User membuat data kegiatan', table: 'project', data: data.id, user: userMobile.id })
        return NextResponse.json({ success: true, message: "Berhasil membuat kegiatan" }, { status: 200 });

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