import type { Request, Response } from "express";
import { DashboardService } from "../services/DashboardService.js";

export class DashboardController {
    constructor(private readonly dashboardService = new DashboardService()) { }

    dashboardData = async (_req: Request, res: Response): Promise<void> => {
        try {
            // Run all queries in parallel for performance
            const [
                totalCustomers,
                totalSellers,
                todaysPurchaseAmount,
                todaysSalesAmount,
                todaysExpensesAmount,
                todaysRetailSalesAmount,
                todaysProfit,
                salesProfitChart,
                pendingVendorPayments,
                pendingCustomerReceipts,
                vendorPaymentsBreakdown,
                customerReceiptsBreakdown,
                vegetableStock,
                topDemandedVegetables,
                recentSales,
                todaysAttendance,
            ] = await Promise.all([
                this.dashboardService.getTotalCustomers(),
                this.dashboardService.getTotalSellers(),
                this.dashboardService.getTodaysPurchaseAmount(),
                this.dashboardService.getTodaysSalesAmount(),
                this.dashboardService.getTodaysExpensesAmount(),
                this.dashboardService.getTodaysRetailSalesAmount(),
                this.dashboardService.getTodaysProfit(),
                this.dashboardService.getSalesProfitChart(),
                this.dashboardService.getPendingVendorPayments(),
                this.dashboardService.getPendingCustomerReceipts(),
                this.dashboardService.getVendorPaymentsBreakdown(),
                this.dashboardService.getCustomerReceiptsBreakdown(),
                this.dashboardService.getVegetableStock(),
                this.dashboardService.getTopDemandedVegetables(),
                this.dashboardService.getRecentSales(),
                this.dashboardService.getTodaysAttendance(),
            ]);

            // Net profit computed by arrival-aware per-item logic (not simple subtraction)
            const todaysNetProfit = todaysProfit - todaysExpensesAmount;

            const data = {
                statCards: {
                    todaysPurchaseAmount,
                    todaysSalesAmount,
                    todaysExpensesAmount,
                    todaysRetailSalesAmount,
                    todaysNetProfit,
                    totalCustomers,
                    totalSellers,
                },
                todaysAttendance,
                salesProfitChart,
                outstandingAccounts: {
                    pendingVendorPayments,
                    pendingCustomerReceipts,
                    vendorPayments: vendorPaymentsBreakdown,
                    customerReceipts: customerReceiptsBreakdown,
                },
                vegetableStock,
                topDemandedVegetables,
                recentSales,
            };

            res.status(200).json({ message: "Dashboard data fetched successfully", data });
        } catch (error) {
            const message = error instanceof Error ? error.message : "Failed to fetch dashboard data";
            res.status(500).json({ message });
        }
    };
}