import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";
import "server-only";

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

export interface InvoiceResultV2 {
    id: number;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    team: string;
    teamId: number;
    teamCodeName: string;
    stage: string;
    paymentMilestone: string;
    paymentMilestoneDate: string;
    client: string;
    address: string;
    attention: string;
    invoiceDate: string;
    receiptDate: string;
    dueDate: number[]; 
    issueAmount: number;
    paidAmount: number;
}

export interface issuedInvoiceResult {
    id: number;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    team: string;
    stage: string;
    paymentMilestone: string;
    paymentMilestoneDate: string;
    client: string;
    address: string;
    attention: string;
    invoiceDate: number[];
    dueDate: number[]; 
    issuedAmount: number;
}

export interface receivedInvoiceResult {
    id: number;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    team: string;
    receiptDate: number[]; 
    receivedAmount: number;
}

export interface invoiceList {
    id: number;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    // stage: string;
    issuedDate: string;
    receiptDate: string;
    issuedAmount: number;
    receivedAmount: number;
    team: string;
    teamCodeName: string;
    settled: boolean;
}

export interface invoiceColum {
    id: number;
    action: string;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    // stage: string;
    issuedDate: string;
    receiptDate: string;
    issuedAmount: string;
    receivedAmount: string;
    team: string;
}


export interface issuedInvoiceList {
    id: number;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    // team: string;
    stage: string;
    paymentMilestone: string;
    // paymentMilestoneDate: string;
    // client: string;
    // address: string;
    // attention: string;
    invoiceDate: string;
    dueDate: string; 
    issuedAmount: string;
}

export interface receivedInvoiceList {
    id: number;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    team: string;
    // stage: string;
    // paymentMilestone: string;
    // paymentMilestoneDate: string;
    // client: string;
    // address: string;
    // attention: string;
    receiptDate: string;
    receivedAmount: string;
}

export interface issuedInvoiceSearchForm {
    id: number;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    team: string;
    // team: string;
    // stage: string;
    // paymentMilestone: string;
    // paymentMilestoneDate: string;
    // client: string;
    // address: string;
    // attention: string;
    invoiceDate: string;
    invoiceDateTo: string;
    dueDate: string; 
    dueDateTo: string; 
    settled: boolean;
    // issuedAmount: string;
}

export interface receivedInvoiceSearchForm {
    id: number;
    invoiceNo: string;
    projectCode: string;
    projectName: string;
    // team: string;
    // stage: string;
    // paymentMilestone: string;
    // paymentMilestoneDate: string;
    // client: string;
    // address: string;
    // attention: string;
    receiptDate: string;
    receiptDateTo: string;
    // dueDate: string; 
    // dueDateTo: string; 
    // issuedAmount: string;
}


export interface InvoiceInformatio{
    id: number;
    address: string;
    attention: string;
    invoiceDate: string;
    dueDate: string;
    projectRefNo: string;
}

export const preloadInvoices = () => {
    fetchInvoices();
};

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

export const fetchIssuedInvoices = cache(async () => {
    return serverFetchJson<issuedInvoiceResult[]>(`${BASE_API_URL}/invoices/v2/allInvoices`, {
      next: { tags: ["invoices"] },
    });
});

export const fetchReceivedInvoices = cache(async () => {
    return serverFetchJson<receivedInvoiceResult[]>(`${BASE_API_URL}/invoices/v2/allInvoices/paid`, {
      next: { tags: ["invoices"] },
    });
});

export const fetchInvoicesV3 = cache(async () => {
    return serverFetchJson<InvoiceResultV2[]>(`${BASE_API_URL}/invoices/v3/allInvoices`, {
      next: { tags: ["invoices"] },
    });
});