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

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

      const { id } = context.params;
      const { link, idDivision } = (await request.json());

      const data = await prisma.divisionProject.count({
         where: {
            id: id,
         },
      });

      if (data == 0) {
         return NextResponse.json(
            {
               success: false,
               message: "Tambah link tugas gagal, data tugas tidak ditemukan",
            },
            { status: 404 }
         );
      }



      const insertlink = await prisma.divisionProjectLink.create({
         data: {
            idProject: id,
            link,
            idDivision,
         }
      })


      // create log user
      const log = await createLogUser({ act: 'CREATE', desc: 'User menambahkan link tugas divisi', table: 'divisionProjectLink', data: insertlink.id })


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


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

      const { idLink } = (await request.json())

      const data = await prisma.divisionProjectLink.count({
         where: {
            id: idLink
         }
      })

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

      const deleteLink = await prisma.divisionProjectLink.update({
         where: {
            id: idLink
         },
         data: {
            isActive: false
         }
      })

      // create log user
      const log = await createLogUser({ act: 'DELETE', desc: 'User menghapus link tugas divisi', table: 'divisionProjectLink', data: String(idLink) })
      return NextResponse.json({ success: true, message: "Berhasil menghapus link tugas divisi" }, { status: 200 });

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