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

export { POST, GET, PUT };

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

  try {
    console.log("[ID INVOICE]", id);
    console.log("[DATA INVOICE]", data);

    const create = await prisma.investasi_Invoice.create({
      data: {
        investasiId: id,
        authorId: data.authorId,
        nominal: "" + data.total,
        lembarTerbeli: "" + data.jumlah,
        masterBankId: data.bankId,
        statusInvoiceId: "3",
      },
    });

    console.log("[CREATE INVOICE]", create);

    return NextResponse.json({
      status: 201,
      success: true,
      message: "Berhasil membuat invoice",
      data: create,
    });
  } catch (error) {
    console.error("[ERROR CREATE INVOICE]", error);
    return NextResponse.json({
      status: 500,
      success: false,
      message: "Gagal membuat invoice",
      reason: (error as Error).message,
    });
  }
}

async function GET(request: Request, { params }: { params: { id: string } }) {
  const { id } = params; // << AUTHOR ID
  const { searchParams } = new URL(request.url);
  const category = searchParams.get("category");
  const authorId = searchParams.get("authorId");
  const page = Number(searchParams.get("page"));
  const takeData = PAGINATION_DEFAULT_TAKE;
  const skipData = page ? page * takeData - takeData : 0;

  console.log("[ID INVOICE]", id);

  let fixData;

  try {
    if (category === "invoice") {
      const data = await prisma.investasi_Invoice.findFirst({
        where: {
          id: id,
        },
        include: {
          MasterBank: true,
          StatusInvoice: true,
          Investasi: {
            include: {
              MasterPembagianDeviden: true,
              MasterPencarianInvestor: true,
              MasterPeriodeDeviden: true,
              ProspektusInvestasi: true,
              Investasi_Invoice: {
                where: {
                  statusInvoiceId: "1",
                },
              },
            },
          },
          Author: {
            include: {
              Profile: true,
            },
          },
        },
      });

      console.log("[DATA INVOICE]", data ? true : false);

      const { ...allData } = data;
      const Investor = data?.Investasi?.Investasi_Invoice;
      fixData = { ...allData, Investor };
    } else if (category == "my-invest") {
      const data = await prisma.investasi_Invoice.findMany({
        orderBy: {
          updatedAt: "desc",
        },
        where: {
          authorId: authorId,
          statusInvoiceId: "1",
          isActive: true,
        },
        take: page ? takeData : undefined,
        skip: page ? skipData : undefined,
        select: {
          id: true,
          nominal: true,
          lembarTerbeli: true,
          Investasi: {
            select: {
              title: true,
              progress: true,
            },
          },
        },
      });

      fixData = data.map((v: any) => ({
        ..._.omit(v, ["Investasi"]),
        title: v.Investasi.title,
        progress: v.Investasi.progress,
      }));
    } else if (category == "transaction") {
      const data = await prisma.investasi_Invoice.findMany({
        orderBy: {
          updatedAt: "desc",
        },
        where: {
          authorId: authorId,
        },
        take: page ? takeData : undefined,
        skip: page ? skipData : undefined,
        select: {
          id: true,
          statusInvoiceId: true,
          nominal: true,
          createdAt: true,
          StatusInvoice: {
            select: {
              name: true,
            },
          },
          Investasi: {
            select: {
              title: true,
            },
          },
        },
      });

      fixData = data.map((v: any) => ({
        ..._.omit(v, ["Investasi", "StatusInvoice"]),
        title: v.Investasi.title,
        statusInvoice: v.StatusInvoice.name,
      }));
    }

    return NextResponse.json({
      status: 200,
      success: true,
      message: `Berhasil mendapatkan data ${category}`,
      data: fixData,
    });
  } catch (error) {
    console.error(`Error get ${category}`, error);
    return NextResponse.json({
      status: 500,
      success: false,
      message: `Gagal mendapatkan data ${category}`,
      reason: (error as Error).message,
    });
  }
}

async function PUT(request: Request, { params }: { params: { id: string } }) {
  const { id } = params;
  const { data } = await request.json();
  const { searchParams } = new URL(request.url);
  const status = searchParams.get("status");
  const fixStatus = _.startCase(status || "");

  console.log("[FIX STATUS]", fixStatus);
  console.log("[DATA]", data);
  console.log("[ID]", id);

  try {
    const checkStatus = await prisma.investasiMaster_StatusInvoice.findFirst({
      where: {
        name: fixStatus,
      },
    });

    console.log("[CHECK STATUS]", checkStatus);

    if (!checkStatus) {
      throw new Error("Status invoice tidak ditemukan");
    }

    const update = await prisma.investasi_Invoice.update({
      where: {
        id: id,
      },
      data: {
        statusInvoiceId: checkStatus.id,
        imageId: data.imageId,
      },
      select: {
        investasiId: true,
      },
    });

    if (fixStatus === "Proses") {
      // kirim notif ke author investasi
      const findInvestasi = await prisma.investasi.findUnique({
        where: {
          id: update.investasiId as any,
        },
        select: {
          title: true,
          authorId: true,
        },
      });

      const findUsers = await prisma.user.findMany({
        where: {
          masterUserRoleId: "2",
          active: true,
          NOT: { id: findInvestasi?.authorId as any },
        },
        select: { id: true },
      });

      // SEND NOTIFICATION
      await sendNotificationMobileToManyUser({
        recipientIds: findUsers.map((user) => user.id),
        senderId: data.authorId,
        payload: {
          title: "Ada Investor Baru !" as NotificationMobileTitleType,
          body: `Cek data investor pada ${findInvestasi?.title}` as NotificationMobileBodyType,
          type: "announcement",
          kategoriApp: "INVESTASI",
          deepLink: routeAdminMobile.investmentDetailPublish({
            id: update.investasiId as string,
            status: "publish",
          }),
        },
      });
    }

    console.log("[UPDATE]", update);

    return NextResponse.json({
      status: 200,
      success: true,
      message: "Berhasil mengubah status invoice",
      // data: update,
    });
  } catch (error) {
    console.error("Error update invoice", error);
    return NextResponse.json({
      status: 500,
      success: false,
      message: "Gagal mengubah status invoice",
      reason: (error as Error).message,
    });
  }
}
