"use server"

import { serverFetchJson, serverFetchString } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";

export interface InvoiceResult {
  id: number;
  projectCode: string;
  projectName: string;
  stage: string;
  comingPaymentMileStone: string;
  paymentMilestoneDate: string;
  resourceUsage: number;
  unbilledHours: number;
  reminder: string;
}

export interface CreateInvoiceInputs {
  id: number;

  // Project Details
  projectCode: string;
  projectName: string;
  stage: string;
  comingPaymentMileStone: string;
  paymentMilestoneDate: string;
  resourceUsage: number;
  unbilledHours: number;

  // Invoice Info
  client: string;
  address: string;
  attention: string;
  invoiceDate: string;
  dueDate: string;
  projectRefNo: string;

  // Invoice related Info
  reminder: string;
  amount: number;
  billHours: number;
}

export interface InvoiceInformation{
  id: number;
  client: string;
  address: string;
  attention: string;
  invoiceDate: string;
  dueDate: string;
  projectRefNo: string;
  
  amount: number;
}

export const fetchProjectInvoiceById = cache(async (id: number) => {
  return serverFetchJson<InvoiceResult[]>(`${BASE_API_URL}/invoices/getProjectDetail/${id}`, {
      next: { tags: ["projectDetailById"] },
    });
})

export const fetchInvoiceInfoById = cache(async (id: number) => {
  return serverFetchJson<InvoiceInformation[]>(`${BASE_API_URL}/invoices/getInvoiceInfo/${id}`, {
      next: { tags: ["invoiceInfoById"] },
    });
})

export const importIssuedInovice = async (data: FormData) => {
  // console.log("----------------",data)
  const importIssuedInovice = await serverFetchJson<any>(
      `${BASE_API_URL}/invoices/import/issued`,
      {
          method: "POST",
          body: data,
          // headers: { "Content-Type": "multipart/form-data" },
      },
  );

  return importIssuedInovice;
};

export const importReceivedInovice = async (data: FormData) => {
  // console.log("----------------",data)
  const importReceivedInovice = await serverFetchJson<any>(
      `${BASE_API_URL}/invoices/import/received`,
      {
          method: "POST",
          body: data,
          // headers: { "Content-Type": "multipart/form-data" },
      },
  );

  return importReceivedInovice;
};