import { FinancialByProject } from "@/app/api/financialsummary";

export type SumOfByTeam = {
    id: number,
    team: string,
    totalFee: number,
    totalBudget: number,
    manhourExpense: number,
    projectExpense: number,
    invoicedAmount: number,
    uninvoicedAmount: number,
    paidAmount: number,
    activeProject: number,
}

export type SumOfByClient = {
    id: number, // custId
    customerCode: string,
    customerName: string,
    totalFee: number,
    totalBudget: number,
    manhourExpense: number,
    projectExpense: number,
    invoicedAmount: number,
    uninvoicedAmount: number,
    paidAmount: number,
    sumOfProjects: number,
}
export function sumUpByClient(data: FinancialByProject[]): SumOfByClient[] {
    const result = data.reduce<Record<number, SumOfByClient>>((acc, item) => {
        if (!acc[item.custId]) {
            acc[item.custId] = {
                id: item.custId,  
                customerCode: item.customerCode,
                customerName: item.customerName,
                totalFee: 0,
                totalBudget: 0,
                manhourExpense: 0,
                projectExpense: 0,
                invoicedAmount: 0,
                uninvoicedAmount: 0,
                paidAmount: 0,
                sumOfProjects: 0
            };
        }
        // Sum the numeric fields
        acc[item.custId].totalFee += item.totalFee;
        acc[item.custId].totalBudget += item.totalBudget;
        acc[item.custId].manhourExpense += item.manhourExpense;
        acc[item.custId].projectExpense += item.projectExpense;
        acc[item.custId].invoicedAmount += item.invoicedAmount;
        acc[item.custId].uninvoicedAmount += item.uninvoicedAmount;
        acc[item.custId].paidAmount += item.paidAmount;
        acc[item.custId].sumOfProjects += 1;

        return acc;
    }, {});
    return Object.values(result);
}
export function sumUpByTeam(data: FinancialByProject[]): SumOfByTeam[] {
    const result = data.reduce<Record<number, SumOfByTeam>>((acc, item) => {
        if (!acc[item.teamId]) {
            acc[item.teamId] = {
                id: item.teamId,  
                team: item.team,
                totalFee: 0,
                totalBudget: 0,
                manhourExpense: 0,
                projectExpense: 0,
                invoicedAmount: 0,
                uninvoicedAmount: 0,
                paidAmount: 0,
                activeProject: 0
            };
        }

        // Sum the numeric fields
        acc[item.teamId].totalFee += item.totalFee;
        acc[item.teamId].totalBudget += item.totalBudget;
        acc[item.teamId].manhourExpense += item.manhourExpense;
        acc[item.teamId].projectExpense += item.projectExpense;
        acc[item.teamId].invoicedAmount += item.invoicedAmount;
        acc[item.teamId].uninvoicedAmount += item.uninvoicedAmount;
        acc[item.teamId].paidAmount += item.paidAmount;
        acc[item.teamId].activeProject += 1;

        return acc;
    }, {});

    return Object.values(result);
}
