"use server"

import { documentType } from "@/prisma/generated/client"

export const createIDUpload = async ({ userId, type, idImageUrlFront, idImageUrlBack }: { userId: string, type: documentType, idImageUrlFront: string, idImageUrlBack?: string }) => {

    try {
        const previous = await prisma?.id_verification.findFirst({
            where: {
                userId,
            }
        })

        if (previous) throw new Error("ID already uploaded")

        const upload = await prisma?.id_verification.create({
            data: {
                userId,
                documentType: type,
                idImageUrlFront,
                idImageUrlBack,
            }
        })

        // create notification item + mark as unread
        const notification = await prisma?.admin_notification.create({
            data: {
                title: "New ID uploaded",
                description: "A new ID has been uploaded for verification.",
                link: "/023115_mbc-admin/dashboard/pages/idverification",
            }
        })

        await prisma?.admin_notification_status.update({
            where: {
                id: 1
            },
            data: {
                newIdVerification: "NOTSEEN",
                status: "NOTSEEN"
            }
        })
        return { data: upload }
    } catch (error) {
        return { status: "fail" }
    }

}