import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib";
import _ from "lodash";
import { sendNotificationMobileToManyUser } from "@/lib/mobile/notification/send-notification";
import {
  NotificationMobileBodyType,
  NotificationMobileTitleType,
} from "../../../../../../../types/type-mobile-notification";
import { routeUserMobile } from "@/lib/mobile/route-page-mobile";
import { funFindDonaturList } from "@/lib/mobile/donation/find-donatur-list";
import { PAGINATION_DEFAULT_TAKE } from "@/lib/constans-value/constansValue";

export { POST, GET, PUT, DELETE };

async function POST(
  request: NextRequest,
  { params }: { params: { id: string } },
) {
  const { id } = params;
  const { data } = await request.json();
  const { title, deskripsi, imageId } = data;

  const senderId = await prisma.donasi.findUnique({
    where: { id: id },
    select: {
      authorId: true,
    },
  });

  try {
    if (data && data?.imageId) {
      const createWithFile = await prisma.donasi_Kabar.create({
        data: {
          title: title,
          deskripsi: deskripsi,
          donasiId: id,
          imageId: imageId,
        },
      });

      if (!createWithFile)
        return NextResponse.json({ status: 400, message: "Gagal disimpan" });
    } else {
      const create = await prisma.donasi_Kabar.create({
        data: {
          title: title,
          deskripsi: deskripsi,
          donasiId: id,
        },
      });

      if (!create)
        return NextResponse.json({ status: 400, message: "Gagal disimpan" });
    }

    const recipientIds = await funFindDonaturList(id);

    // SEND NOTIFICATION
    if (recipientIds.length > 0) {
      await sendNotificationMobileToManyUser({
        recipientIds,
        senderId: senderId?.authorId!,
        payload: {
          title: "Berita terbaru" as NotificationMobileTitleType,
          body: `Ada berita terupdate pada ${title}` as NotificationMobileBodyType,
          type: "announcement",
          kategoriApp: "DONASI",
          deepLink: routeUserMobile.donationDetailPublish({
            id: id,
          }),
        },
      });
    }

    return NextResponse.json({
      status: 200,
      success: true,
      message: "Berhasil membuat kabar",
    });
  } catch (error) {
    console.error("[ERROR CREATE NEWS]", error);
    return NextResponse.json({
      status: 500,
      success: false,
      message: "Error Create Donation News",
      reason: (error as Error).message,
    });
  }
}

async function GET(
  request: NextRequest,
  { params }: { params: { id: string } },
) {
  const { id } = params;
  const { searchParams } = new URL(request.url);
  const category = searchParams.get("category");
  const page = Number(searchParams.get("page")) || 1; // Default page 1 jika tidak ada
  const takeData = PAGINATION_DEFAULT_TAKE; // Default 10 data per halaman
  const skipData = page * takeData - takeData;
  
  let fixData;
  let totalCount = 0; // Untuk menghitung total data

  try {
    if (category === "get-all") {
      const data = await prisma.donasi_Kabar.findMany({
        orderBy: {
          updatedAt: "desc",
        },
        where: {
          donasiId: id,
          active: true,
        },
        take: page ? takeData : undefined,
        skip: page ? skipData : undefined,
        select: {
          id: true,
          title: true,
          deskripsi: true,
          createdAt: true,
        },
      });

      // Hitung total data untuk pagination
      totalCount = await prisma.donasi_Kabar.count({
        where: {
          donasiId: id,
          active: true,
        },
      });

      fixData = data;

    } else if (category === "get-one") {
      const data = await prisma.donasi_Kabar.findUnique({
        where: {
          id: id,
        },
        include: {
          Donasi: {
            select: {
              authorId: true,
            },
          },
        },
      });

      const authorId = data?.Donasi?.authorId;

      fixData = {
        ..._.omit(data, ["Donasi"]),
        authorId: authorId,
      };
    }

    // Hitung total halaman jika kategori adalah get-all
    let pagination = undefined;
    if (category === "get-all") {
      const totalPages = Math.ceil(totalCount / takeData);
      pagination = {
        currentPage: page,
        totalPages: totalPages,
        totalData: totalCount,
        dataPerPage: takeData,
      };
    }

    return NextResponse.json({
      status: 200,
      success: true,
      message: "Berhasil mengambil kabar",
      data: fixData,
      pagination: pagination,
    });
  } catch (error) {
    console.error("[ERROR GET NEWS]", error);
    return NextResponse.json({
      status: 500,
      success: false,
      message: "Error Get Donation News",
      reason: (error as Error).message,
    });
  }
}

async function PUT(request: Request, { params }: { params: { id: string } }) {
  const { id } = params;
  const { data } = await request.json();

  try {
    if (data && data.newImageId) {
      const updateWithImage = await prisma.donasi_Kabar.update({
        where: {
          id: id,
        },
        data: {
          title: data.title,
          deskripsi: data.deskripsi,
          imageId: data.newImageId,
        },
      });

      if (!updateWithImage)
        return NextResponse.json({
          status: 400,
          success: false,
          message: "Gagal Update",
        });
    } else {
      const updateData = await prisma.donasi_Kabar.update({
        where: {
          id: id,
        },
        data: {
          title: data.title,
          deskripsi: data.deskripsi,
        },
      });

      if (!updateData)
        return NextResponse.json({
          status: 400,
          success: false,
          message: "Gagal Update",
        });
    }
    return NextResponse.json({
      status: 200,
      success: true,
      message: "Berhasil Update",
    });
  } catch (error) {
    console.error("[ERROR UPDATE NEWS]", error);
    return NextResponse.json({
      status: 500,
      success: false,
      message: "Error Update Donation News",
      reason: (error as Error).message,
    });
  }
}

async function DELETE(
  request: Request,
  { params }: { params: { id: string } },
) {
  const { id } = params;
  try {
    const deleteData = await prisma.donasi_Kabar.delete({
      where: {
        id: id,
      },
      select: {
        imageId: true,
      },
    });

    const deleteImage = await fetch(
      `https://wibu-storage.wibudev.com/api/files/${deleteData?.imageId}/delete`,
      {
        method: "DELETE",
        headers: {
          Authorization: `Bearer ${process.env.WS_APIKEY}`,
        },
      },
    );

    if (!deleteImage) {
      console.log("[FAILED DELETE IMAGE]", deleteImage);
    }

    return NextResponse.json({
      status: 200,
      success: true,
      message: "Berhasil Delete",
    });
  } catch (error) {
    console.error("[ERROR DELETE NEWS]", error);
    return NextResponse.json({
      status: 500,
      success: false,
      message: "Error Delete Donation News",
      reason: (error as Error).message,
    });
  }
}
