
// USER DEFINED FUNCTION TO MAKE GRADIENT COLORS FOR THE CHARTS
function gradientMaker(count, startColor, endColor) {
    const colors = [];
    if (count == 1) {
        return 'rgb(229, 84, 81)';
    }
    for (let i = 0; i < count; i++) {
        const r = Math.round(startColor[0] + (endColor[0] - startColor[0]) * (i / (count - 1)));
        const g = Math.round(startColor[1] + (endColor[1] - startColor[1]) * (i / (count - 1)));
        const b = Math.round(startColor[2] + (endColor[2] - startColor[2]) * (i / (count - 1)));
        colors.push(`rgb(${r}, ${g}, ${b})`);
    }

    return colors;
}

function formatIndianNumber(value) {
    if (value >= 10000000) { 
        return (value / 10000000).toFixed(2) + " Cr";
    } else if (value >= 100000) { 
        return (value / 100000).toFixed(2) + " L";
    } else if (value >= 1000) { 
        return (value / 1000).toFixed(2) + "K";
    } else {
        return value.toLocaleString();
    }
}

Chart.register(ChartDataLabels);

function initChart(id) {
    const ctx = document.getElementById(id);
    if (!ctx) return;

    if (chartRegistry[id]) {
        chartRegistry[id].destroy();
        delete chartRegistry[id];
    }

    const baseId = id.replace('_modal', '');
    let chartType = "bar";
    let chartData = {};
    let chartOptions = {
        responsive: true,
        plugins: {
            legend: { 
                display: false,
                labels: {
                    color: 'white'
                }
            },
            datalabels: {
                color: 'white',
                font: { weight: 'bold' },
                formatter: (value, context) => {
                    if (baseId === "averageDeliveryDays") return value + " days";
                    if (baseId.includes("revenue") || baseId.includes("Revenue")) return "₹" + formatIndianNumber(value);
                    return value;
                }
            }
        },
        scales: {
            y: {
                beginAtZero: true,
                ticks: { color: 'white' }
            },
            x: {
                ticks: { color: 'white' }
            }
        }
    };

    switch (baseId) {
        case "revenueByCategory":
            chartType = "doughnut";
            chartData = {
                labels: categories,
                datasets: [{
                    data: revenue_by_categories,
                    backgroundColor: ["#00C49F", "#0088FE", "#FFBB28", "#FF8042", "#A028FF"]
                }]
            };
            chartOptions.plugins.legend.display = true;
            delete chartOptions.scales;
            break;

        case "ordersByStatus":
            chartData = {
                labels: order_status,
                datasets: [{
                    data: order_count,
                    backgroundColor: ["#00C49F","#FFBB28","#FF4444","#0088FE","#A028FF","#FF8042","#4CAF50","#E91E63","#9C27B0","#795548","#00BCD4","#607D8B"]
                }]
            };
            break;

        case "revenueByPayment":
            chartData = {
                labels: payment_methods,
                datasets: [{
                    data: revenue_by_payment,
                    backgroundColor: ["#00C49F","#0088FE","#FFBB28","#FF4444","#A028FF","#FF8042","#4CAF50","#E91E63","#00BCD4"]
                }]
            };
            chartOptions.indexAxis = 'y';
            break;

        case "ordersByGender":
            chartType = "doughnut";
            chartData = {
                labels: genders,
                datasets: [{
                    data: order_count_by_gender,
                    backgroundColor: ["#FF6384","#36A2EB","#FFCE56"]
                }]
            };
            chartOptions.plugins.legend.display = true;
            delete chartOptions.scales;
            break;

        case "topCitiesRevenue":
            chartData = {
                labels: top_cities,
                datasets: [{
                    data: top_revenue,
                    backgroundColor: ["#00C49F","#FFBB28","#FF4444","#0088FE","#A028FF","#FF8042","#4CAF50","#E91E63","#9C27B0","#795548","#00BCD4","#607D8B"]
                }]
            };
            break;

        case "averageDeliveryDays":
            chartData = {
                labels: courier_partners,
                datasets: [{
                    data: average_delivery_days,
                    backgroundColor: ["#36A2EB", "#BA55D3", "#FF69B4", "#FF4444"]
                }]
            };
            chartOptions.indexAxis = 'y';
            break;
    }

    chartRegistry[id] = new Chart(ctx, {
        type: chartType,
        data: chartData,
        options: chartOptions,
        plugins: [ChartDataLabels]
    });
}

