/**
 * Price Grid Fix - Enforces price interval and grid line constraints
 * This module ensures all price generation respects asset-specific intervals
 */

// Override the chart's price calculation methods to respect intervals
(function() {
    'use strict';

    // Wait for chart to be initialized
    const initPriceGridFix = () => {
        if (!window.SmoothTradingChart) {
            setTimeout(initPriceGridFix, 100);
            return;
        }

        const originalPrototype = window.SmoothTradingChart.prototype;

        // Override getPriceRange to respect asset intervals
        const originalGetPriceRange = originalPrototype.getPriceRange;
        originalPrototype.getPriceRange = function() {
            const result = originalGetPriceRange.call(this);
            
            // Get asset config
            const assetConfig = this.getAssetConfig();
            if (!assetConfig) return result;

            // Force interval to match asset configuration
            const priceIntervals = assetConfig.priceInterval;
            result.interval = assetConfig.baseInterval;
            
            // Ensure interval is from allowed list
            if (!priceIntervals.includes(result.interval)) {
                result.interval = priceIntervals[0];
            }

            return result;
        };

        // Override calculateDynamicPriceRange to respect grid limits
        originalPrototype.calculateDynamicPriceRange = function(centerPrice, interval, targetLines) {
            const assetConfig = this.getAssetConfig();
            
            // Force 7 grid lines maximum
            const maxLines = 7;
            const actualLines = Math.min(targetLines || 7, maxLines);
            
            // Use asset-specific interval
            const actualInterval = assetConfig ? assetConfig.baseInterval : interval;
            
            // Calculate range based on grid lines
            const halfLines = Math.floor(actualLines / 2);
            const min = centerPrice - (halfLines * actualInterval);
            const max = centerPrice + (halfLines * actualInterval);
            
            return { min, max, interval: actualInterval };
        };

        // Override checkAndAdjustPriceInterval to prevent dynamic changes
        originalPrototype.checkAndAdjustPriceInterval = function() {
            const assetConfig = this.getAssetConfig();
            if (!assetConfig) return;

            // Lock to base interval - no dynamic adjustment
            this.currentInterval = assetConfig.baseInterval;
            this.minInterval = assetConfig.baseInterval;
            this.maxInterval = assetConfig.priceInterval[assetConfig.priceInterval.length - 1];
            
            // Force 7 grid lines
            this.priceLineCount = 7;
            this.optimalMaxLines = 7;
            this.maxPriceLines = 7;
        };

        // Override priceToY to ensure alignment with grid
        const originalPriceToY = originalPrototype.priceToY;
        originalPrototype.priceToY = function(price) {
            const assetConfig = this.getAssetConfig();
            if (!assetConfig) return originalPriceToY.call(this, price);

            // Round price to nearest interval
            const interval = assetConfig.baseInterval;
            const roundedPrice = Math.round(price / interval) * interval;
            
            return originalPriceToY.call(this, roundedPrice);
        };

        console.log('✅ Price Grid Fix Applied - Intervals and grid lines locked to asset config');
    };

    // Initialize when DOM is ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initPriceGridFix);
    } else {
        initPriceGridFix();
    }
})();
