/**
 * Keiste Solar Analysis - Chart Utilities
 * Chart.js integration for energy and break-even visualizations
 * Version: 1.1.0
 */

(function () {
    'use strict';

    const SOLAR_PANEL_DEGRADATION = 0.005;
    const YRS_OF_SYSTEM = 25;
    const DAY_POWER_AVG = 1.85;
    const DAYS_IN_YR = 365.4;

    // ===== HELPER FUNCTIONS =====
    function formatCurrency(value, decimals = 0) {
        const num = Number(value) || 0;
        return (window.CURRENCY_SYMBOL || '') + num.toLocaleString('en-IE', {
            minimumFractionDigits: decimals,
            maximumFractionDigits: decimals
        });
    }

    // ===== BREAK-EVEN CHART =====
    function calculateBreakEvenDataSimple(config) {
        const yearlyEnergy = config.yearlyEnergyDcKwh;
        const panelCount = config.panelsCount;
        
        // Get values from config or inputs
        const cfg = window.KSRAD_CalcConfig || {};
        const electricityRate = parseFloat(document.getElementById('electricityRate')?.value) || 0;
        const feedInTariff = parseFloat(cfg.defaultFeedInTariff || 0);
        const billMonthly = parseFloat(document.getElementById('electricityBill')?.value) || 0;

        const exportRatePercent = parseFloat(cfg.defaultExportRate || 0) / 100;

        const inclGrant = document.getElementById('inclGrant')?.checked;
        const inclLoan = document.getElementById('inclLoan')?.checked;
        const degradation = SOLAR_PANEL_DEGRADATION;
        const annualIncrease = parseFloat(cfg.annualPriceIncrease || 0) / 100;

        // Get actual net install cost from the calculator
        // We need to recalculate it here to match what keyFigures does
        const baseCost = config.installCost || 0;
        
        // Get grant settings based on building type
        const getGrantAmount = () => {
            try {
                const grants = cfg.grantSettings || {};
                const btEl = document.getElementById('userBuildingType');
                const selected = (btEl && btEl.value) ? btEl.value.trim() : 'Residential';
                const isResidential = (selected === 'Residential');
                const category = isResidential ? 'Residential' : 'Non-Residential';
                const grantSettings = grants[category] || { 
                    rate: isResidential ? parseFloat(cfg.grantRateDomestic || 0) / 100 : parseFloat(cfg.grantRateNonDomestic || 0) / 100,
                    cap: isResidential ? parseFloat(cfg.grantCapDomestic || 0) : parseFloat(cfg.grantCapNonDomestic || 0)
                };
                return inclGrant ? Math.min(baseCost * grantSettings.rate, grantSettings.cap) : 0;
            } catch (e) {
                const isRes = true;
                const rate = parseFloat(cfg.grantRateDomestic || 0) / 100;
                const cap = parseFloat(cfg.grantCapDomestic || 0);
                return inclGrant ? Math.min(baseCost * rate, cap) : 0;
            }
        };
        
        const seaiGrant = getGrantAmount();
        const principal = Math.max(0, baseCost - seaiGrant);
        
        // Calculate loan payments if applicable
        const loanAPR = parseFloat(cfg.defaultLoanApr || 0) / 100;
        const loanYears = parseInt(cfg.loanTerm || 7);
        let yearlyLoanCost = 0;
        let totalCost = principal;
        
        if (inclLoan) {
            const m = 12;
            const n = loanYears * m;
            const r = loanAPR / m;
            const monthlyRepay = principal * (r / (1 - Math.pow(1 + r, -n)));
            yearlyLoanCost = monthlyRepay * 12;
            totalCost = yearlyLoanCost * loanYears;
        }
        
        // Calculate annual usage from bill
        const annualUsageKwh = (billMonthly * 12) / electricityRate;

        const years = Array.from({ length: 25 }, (_, i) => i);
        const savings = years.map(year => {
            const yearDegradation = Math.pow(1 - degradation, year);
            const yearlyEnergyProduction = yearlyEnergy * yearDegradation;
            
            // Calculate self-consumption and export (same logic as main calculator)
            const maxSelfConsumption = yearlyEnergyProduction * (1 - exportRatePercent);
            const actualSelfConsumption = Math.min(maxSelfConsumption, annualUsageKwh);
            const exportedEnergy = yearlyEnergyProduction - actualSelfConsumption;
            
            // Apply escalating electricity rate
            const escalatedRate = electricityRate * Math.pow(1 + annualIncrease, year);
            
            // Year's savings
            const yearlySaving = (actualSelfConsumption * escalatedRate) + (exportedEnergy * feedInTariff);
            
            // Year's loan cost (only for first 7 years if loan)
            const yearLoanCost = (inclLoan && year < loanYears) ? yearlyLoanCost : 0;
            
            // Net saving for this year
            const netYearlySaving = yearlySaving - yearLoanCost;

            // Cumulative savings up to this year
            const totalSaving = year === 0 ? -totalCost : years
                .slice(1, year + 1)
                .reduce((acc, y) => {
                    const yDegradation = Math.pow(1 - degradation, y);
                    const yEnergyProduction = yearlyEnergy * yDegradation;
                    const yMaxSelfConsumption = yEnergyProduction * (1 - exportRatePercent);
                    const yActualSelfConsumption = Math.min(yMaxSelfConsumption, annualUsageKwh);
                    const yExported = yEnergyProduction - yActualSelfConsumption;
                    const yEscalatedRate = electricityRate * Math.pow(1 + annualIncrease, y);
                    const ySaving = (yActualSelfConsumption * yEscalatedRate) + (yExported * feedInTariff);
                    const yLoanCost = (inclLoan && y < loanYears) ? yearlyLoanCost : 0;
                    return acc + (ySaving - yLoanCost);
                }, -totalCost);

            return totalSaving;
        });
        
        const breakEvenYear = savings.findIndex(saving => saving >= 0);

        return {
            cost: totalCost,
            savings: savings,
            breakEvenYear: breakEvenYear
        };
    }

    // ===== INITIALIZE BREAK-EVEN CHART =====
    function initBreakEvenChart() {
        const canvas = document.getElementById('breakEvenChart');
        if (!canvas || !window.Chart) return null;

        const ctx = canvas.getContext('2d');
        
        // Initialize with zero data - will be populated when user enters bill
        const data = {
            cost: 0,
            savings: Array(25).fill(0),
            breakEvenYear: -1
        };

        return new Chart(ctx, {
            type: 'line',
            data: {
                labels: Array.from({ length: 25 }, (_, i) => `Year ${i}`),
                datasets: [{
                    label: '0 Panels',
                    data: data.savings,
                    borderColor: 'rgba(42, 157, 143, 1)',
                    backgroundColor: 'rgba(42, 157, 143, 0.08)',
                    fill: false,
                    tension: 0.3,
                    pointRadius: window.innerWidth > 600 ? 2 : 0,
                    pointHoverRadius: 4,
                    borderWidth: 3
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    title: {
                        display: window.innerWidth > 600,
                        text: window.innerWidth > 600 ? 'Investment Return Over Time' : '',
                        font: {
                            size: window.innerWidth > 600 ? 16 : 12,
                            family: "'Inter', sans-serif",
                            weight: '600'
                        }
                    },
                    legend: { display: false },
                    tooltip: {
                        callbacks: {
                            label: function (context) {
                                const value = context.raw;
                                return value >= 0
                                    ? `Profit: ${formatCurrency(value, 0)}`
                                    : `Investment: ${formatCurrency(Math.abs(value), 0)}`;
                            }
                        }
                    }
                },
                scales: {
                    y: {
                        title: {
                            display: window.innerWidth > 600,
                            text: window.innerWidth > 600 ? 'Net Financial Position (' + (window.CURRENCY_SYMBOL || '') + ')' : '',
                            font: {
                                weight: '500',
                                size: window.innerWidth > 600 ? 14 : 11
                            }
                        },
                        ticks: {
                            callback: function (value) {
                                return (window.CURRENCY_SYMBOL || '') + value.toLocaleString('en-IE', { maximumFractionDigits: 0 });
                            },
                            font: { size: window.innerWidth > 600 ? 13 : 10 }
                        },
                        grid: { display: window.innerWidth > 600 }
                    },
                    x: {
                        title: {
                            display: window.innerWidth > 600,
                            text: window.innerWidth > 600 ? 'Years' : '',
                            font: {
                                weight: '500',
                                size: window.innerWidth > 600 ? 14 : 11
                            }
                        },
                        ticks: {
                            font: { size: window.innerWidth > 600 ? 13 : 10 },
                            autoSkip: true,
                            maxTicksLimit: window.innerWidth > 600 ? 12 : 6
                        },
                        grid: { display: window.innerWidth > 600 }
                    }
                }
            }
        });
    }

    // ===== INITIALIZE ENERGY CHART =====
    function initEnergyChart() {
        const canvas = document.getElementById('energyChart');
        if (!canvas || !window.Chart) return null;

        const ctx = canvas.getContext('2d');

        return new Chart(ctx, {
            type: 'bar',
            data: {
                labels: Array.from({ length: 25 }, (_, i) => `Year ${i}`),
                datasets: [{
                    label: 'Projected Annual Energy Production (kWh)',
                    data: Array.from({ length: 25 }, () => 0),
                    backgroundColor: 'rgba(40, 167, 69, 0.6)',
                    borderColor: 'rgba(40, 167, 69, 1)',
                    borderWidth: 1
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                scales: {
                    y: {
                        beginAtZero: true,
                        title: {
                            display: true,
                            text: 'Annual Energy Production (kWh)'
                        }
                    },
                    x: {
                        ticks: {
                            autoSkip: true,
                            maxTicksLimit: window.innerWidth > 600 ? 12 : 6
                        }
                    }
                },
                plugins: {
                    title: {
                        display: true,
                        text: 'Projected Energy Production (per year)'
                    }
                }
            }
        });
    }

    // ===== UPDATE ENERGY CHART =====
    window.updateEnergyChart = function () {
        try {
            if (!window.energyChart) return;
            
            const panelCount = parseInt(document.getElementById('panelCount')?.value || 4);
            const yearlyEnergy = typeof estimateEnergyProduction === 'function'
                ? estimateEnergyProduction(panelCount)
                : (panelCount * DAY_POWER_AVG * DAYS_IN_YR);
            
            const degradation = parseFloat(document.getElementById('degradation')?.value) / 100 || 0.005;
            const years = Array.from({ length: 25 }, (_, i) => i);
            
            const data = years.map(year => {
                return yearlyEnergy * Math.pow(1 - degradation, year);
            });

            window.energyChart.data.datasets[0].data = data;
            window.energyChart.data.datasets[0].label = `${panelCount} panels — Annual Production (kWh)`;
            window.energyChart.update();
        } catch (e) {
            // Error in energy chart update
        }
    };

    // ===== UPDATE BREAK-EVEN CHART =====
    window.updateBreakEvenChart = function (state, figs) {
        if (!window.breakEvenChart) {
            return;
        }

        try {
            // On page load with no bill, show zero baseline
            if (!state.billMonthly || state.billMonthly === 0) {
                if (window.breakEvenChart.data && window.breakEvenChart.data.datasets && window.breakEvenChart.data.datasets[0]) {
                    window.breakEvenChart.data.datasets[0].data = Array(25).fill(0);
                    window.breakEvenChart.data.datasets[0].label = '0 Panels';
                    window.breakEvenChart.update('none');
                }
                return;
            }

            if (typeof calculateBreakEvenDataSimple !== 'function') return;
            
            const cfg = {
                panelsCount: state.panels,
                yearlyEnergyDcKwh: figs.yearlyEnergyKWh || (state.panels * DAY_POWER_AVG * DAYS_IN_YR)
            };
            
            const be = calculateBreakEvenDataSimple(cfg);
            
            if (be && be.savings && window.breakEvenChart.data &&
                window.breakEvenChart.data.datasets &&
                window.breakEvenChart.data.datasets[0]) {
                
                window.breakEvenChart.data.datasets[0].data = [...be.savings];
                window.breakEvenChart.data.datasets[0].label = `${state.panels} Panels`;
                window.breakEvenChart.update('active');
            }
        } catch (e) {
            // Error in break-even chart update
        }
    };

    // ===== INITIALIZATION =====
    document.addEventListener('DOMContentLoaded', function () {
        if (window.Chart) {
            window.breakEvenChart = initBreakEvenChart();
            window.energyChart = initEnergyChart();
            
            // Don't update charts on initial load - keep values at zero
            // if (window.energyChart) {
            //     setTimeout(window.updateEnergyChart, 100);
            // }
        }
    });

    // Expose functions
    window.calculateBreakEvenDataSimple = calculateBreakEvenDataSimple;
    window.initBreakEvenChart = initBreakEvenChart;
    window.initEnergyChart = initEnergyChart;

})();
