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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
"""

Forecasting models for AgriPredict Analysis Service

Includes comprehensive accuracy metrics: MAE, RMSE, MAPE, Bias, MASE, R-Squared

Supports weighted ensemble based on model performance

"""

import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from typing import List, Dict, Any, Optional, Tuple
from dataclasses import dataclass, field
import asyncio
from concurrent.futures import ThreadPoolExecutor
import traceback
import os
import json

# Import ML libraries (lazy loading to avoid startup issues)
STATS_MODELS_AVAILABLE = True
CATBOOST_AVAILABLE = True
CATBOOST_MODEL_LOADED = False
_catboost_model = None
_catboost_feature_names = None
_catboost_metrics = None

def _import_statsmodels():
    """Lazy import of statsmodels"""
    global ExponentialSmoothing, ARIMA, STATS_MODELS_AVAILABLE
    try:
        if 'ExponentialSmoothing' not in globals():
            from statsmodels.tsa.holtwinters import ExponentialSmoothing
            from statsmodels.tsa.arima.model import ARIMA
    except ImportError:
        STATS_MODELS_AVAILABLE = False
        logger.warning("Statsmodels not available")

def _import_catboost():
    """Lazy import of CatBoost"""
    global CatBoostRegressor, CATBOOST_AVAILABLE
    try:
        if 'CatBoostRegressor' not in globals():
            from catboost import CatBoostRegressor
    except ImportError:
        CATBOOST_AVAILABLE = False
        logger.warning("CatBoost not available")

def _load_catboost_model():
    """Load trained CatBoost model"""
    global _catboost_model, _catboost_feature_names, _catboost_metrics, CATBOOST_MODEL_LOADED
    
    if CATBOOST_MODEL_LOADED:
        return _catboost_model is not None
    
    try:
        import joblib
        model_path = os.path.join(os.path.dirname(__file__), 'catboost_model.pkl')
        
        if os.path.exists(model_path):
            model_data = joblib.load(model_path)
            _catboost_model = model_data.get('model')
            _catboost_feature_names = model_data.get('feature_names', [])
            _catboost_metrics = model_data.get('metrics', {})
            CATBOOST_MODEL_LOADED = True
            logger.info(f"Loaded trained CatBoost model from {model_path}")
            logger.info(f"Model metrics: {_catboost_metrics}")
            return True
        else:
            logger.warning(f"CatBoost model not found at {model_path}")
            CATBOOST_MODEL_LOADED = True  # Mark as attempted
            return False
    except Exception as e:
        logger.error(f"Failed to load CatBoost model: {e}")
        CATBOOST_MODEL_LOADED = True
        return False

from utils.logger import setup_logger
from utils.config import settings

logger = setup_logger(__name__)


class ForecastMetrics:
    """Comprehensive forecast accuracy metrics calculator"""
    
    @staticmethod
    def calculate_all_metrics(y_true: np.ndarray, y_pred: np.ndarray, 

                               y_train: Optional[np.ndarray] = None) -> Dict[str, Optional[float]]:
        """

        Calculate all forecast accuracy metrics

        

        Args:

            y_true: Actual values

            y_pred: Predicted values

            y_train: Training data (for MASE calculation)

            

        Returns:

            Dictionary with all metrics

        """
        from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
        
        y_true = np.array(y_true).flatten()
        y_pred = np.array(y_pred).flatten()
        
        # Remove any NaN or infinite values
        mask = np.isfinite(y_true) & np.isfinite(y_pred)
        y_true = y_true[mask]
        y_pred = y_pred[mask]
        
        if len(y_true) == 0 or len(y_true) != len(y_pred):
            return {
                'mae': None, 'rmse': None, 'mape': None,
                'bias': None, 'mase': None, 'r_squared': None
            }
        
        try:
            # MAE - Mean Absolute Error
            mae = mean_absolute_error(y_true, y_pred)
            
            # RMSE - Root Mean Squared Error
            rmse = np.sqrt(mean_squared_error(y_true, y_pred))
            
            # MAPE - Mean Absolute Percentage Error (handle zero values)
            non_zero_mask = y_true != 0
            if np.any(non_zero_mask):
                mape = np.mean(np.abs((y_true[non_zero_mask] - y_pred[non_zero_mask]) / y_true[non_zero_mask])) * 100
            else:
                mape = None
            
            # Bias - Mean Forecast Error (MFE)
            bias = np.mean(y_pred - y_true)
            
            # MASE - Mean Absolute Scaled Error
            mase = None
            if y_train is not None and len(y_train) > 1:
                y_train = np.array(y_train).flatten()
                naive_errors = np.abs(np.diff(y_train))
                scaling_factor = np.mean(naive_errors)
                if scaling_factor > 0:
                    mase = mae / scaling_factor
            
            # R-Squared
            r_squared = r2_score(y_true, y_pred)
            
            return {
                'mae': round(float(mae), 4),
                'rmse': round(float(rmse), 4),
                'mape': round(float(mape), 4) if mape is not None else None,
                'bias': round(float(bias), 4),
                'mase': round(float(mase), 4) if mase is not None else None,
                'r_squared': round(float(r_squared), 4)
            }
        except Exception as e:
            logger.error(f"Error calculating metrics: {e}")
            return {
                'mae': None, 'rmse': None, 'mape': None,
                'bias': None, 'mase': None, 'r_squared': None
            }


@dataclass
class ForecastResult:
    """Container for forecast results"""
    values: List[float]
    confidence_lower: Optional[List[float]] = None
    confidence_upper: Optional[List[float]] = None
    model_name: str = ""
    metrics: Optional[Dict[str, Optional[float]]] = field(default_factory=dict)
    weight: float = 1.0  # Weight for ensemble (based on accuracy)

class ForecastEngine:
    """Main forecasting engine with multiple models and weighted ensemble"""

    def __init__(self):
        self.logger = logger
        self.executor = ThreadPoolExecutor(max_workers=4)
        self.model_weights = {}  # Store model weights based on historical performance
        self.model_metrics = {}  # Store metrics for each model
        
        # Try to load CatBoost model on init
        _load_catboost_model()

    async def generate_forecast(

        self,

        df: pd.DataFrame,

        days: int,

        models: List[str],

        include_confidence: bool = True,

        scenario: str = "realistic",

        calculate_metrics: bool = True

    ) -> Dict[str, Any]:
        """

        Generate forecast using specified models



        Args:

            df: Historical data DataFrame

            days: Number of days to forecast

            models: List of model names to use

            include_confidence: Whether to include confidence intervals

            scenario: Forecast scenario (optimistic, pessimistic, realistic)



        Returns:

            Dictionary with forecast results

        """
        try:
            self.logger.info(f"Generating {days}-day forecast using models: {models}")

            # Apply scenario adjustment
            scenario_multiplier = self._get_scenario_multiplier(scenario)
            adjusted_df = self._apply_scenario_adjustment(df, scenario_multiplier)

            # Calculate metrics using holdout validation if we have enough data
            if calculate_metrics and len(adjusted_df) > days + 10:
                self._calculate_model_metrics(adjusted_df, days, models)

            # Generate model forecasts
            model_results = await self._generate_model_forecasts(adjusted_df, days, models, include_confidence)

            # Handle fallback if no models succeeded
            if not model_results:
                model_results = self._handle_fallback_forecast(adjusted_df, days)

            # Generate weighted ensemble if requested
            if self._should_generate_ensemble(models):
                ensemble_result = self._generate_weighted_ensemble_forecast(model_results, days, include_confidence)
                model_results['Ensemble'] = ensemble_result

            # Prepare final forecast data with metrics
            final_forecast = self._prepare_forecast_data(model_results, adjusted_df, days)

            return {
                "forecast_data": final_forecast,
                "models_used": list(model_results.keys()),
                "scenario": scenario,
                "model_metrics": self.model_metrics,
                "model_weights": self.model_weights
            }

        except Exception as e:
            self.logger.error(f"Forecast generation failed: {str(e)}")
            raise
    
    def _calculate_model_metrics(self, df: pd.DataFrame, forecast_days: int, models: List[str]):
        """Calculate accuracy metrics for each model using holdout validation"""
        try:
            # Use last 'forecast_days' as holdout set
            train_df = df.iloc[:-forecast_days].copy()
            test_df = df.iloc[-forecast_days:].copy()
            
            if len(train_df) < 10:
                self.logger.warning("Not enough data for metric calculation")
                return
            
            y_true = test_df['price'].values
            y_train = train_df['price'].values
            
            for model_name in models:
                if model_name.lower() == 'ensemble':
                    continue
                    
                try:
                    method_name = f'_generate_{model_name.lower()}_forecast'
                    if hasattr(self, method_name):
                        result = getattr(self, method_name)(train_df.copy(), forecast_days, False)
                        if result and result.values:
                            y_pred = np.array(result.values[:len(y_true)])
                            metrics = ForecastMetrics.calculate_all_metrics(y_true, y_pred, y_train)
                            self.model_metrics[model_name] = metrics
                            
                            # Calculate weight based on MAE (lower is better)
                            if metrics.get('mae') is not None and metrics['mae'] > 0:
                                self.model_weights[model_name] = 1.0 / metrics['mae']
                            else:
                                self.model_weights[model_name] = 1.0
                except Exception as e:
                    self.logger.warning(f"Failed to calculate metrics for {model_name}: {e}")
                    self.model_weights[model_name] = 1.0
            
            # Normalize weights
            total_weight = sum(self.model_weights.values())
            if total_weight > 0:
                self.model_weights = {k: v / total_weight for k, v in self.model_weights.items()}
                
            self.logger.info(f"Calculated model weights: {self.model_weights}")
            
        except Exception as e:
            self.logger.error(f"Error calculating model metrics: {e}")

    def _apply_scenario_adjustment(self, df: pd.DataFrame, multiplier: float) -> pd.DataFrame:
        """Apply scenario multiplier to price data"""
        adjusted_df = df.copy()
        adjusted_df['price'] = adjusted_df['price'] * multiplier
        return adjusted_df

    async def _generate_model_forecasts(

        self,

        df: pd.DataFrame,

        days: int,

        models: List[str],

        include_confidence: bool

    ) -> Dict[str, ForecastResult]:
        """Generate forecasts from individual models"""
        forecast_tasks = []
        model_results = {}

        # Create forecast tasks for each model
        for model_name in models:
            if model_name.lower() != 'ensemble' and hasattr(self, f'_generate_{model_name.lower()}_forecast'):
                task = asyncio.get_event_loop().run_in_executor(
                    self.executor,
                    getattr(self, f'_generate_{model_name.lower()}_forecast'),
                    df.copy(),
                    days,
                    include_confidence
                )
                forecast_tasks.append((model_name, task))

        # Execute all forecast tasks
        if forecast_tasks:
            results = await asyncio.gather(*[task for _, task in forecast_tasks], return_exceptions=True)

            for (model_name, _), result in zip(forecast_tasks, results):
                if isinstance(result, Exception):
                    self.logger.warning(f"Model {model_name} failed: {str(result)}")
                    continue

                if result and result.values:
                    model_results[model_name] = result

        return model_results

    def _handle_fallback_forecast(self, df: pd.DataFrame, days: int) -> Dict[str, ForecastResult]:
        """Handle fallback when all models fail"""
        self.logger.warning("All models failed, using fallback forecast")
        fallback_result = self._generate_fallback_forecast(df, days)
        return {'Fallback': fallback_result}

    def _should_generate_ensemble(self, models: List[str]) -> bool:
        """Check if ensemble forecast should be generated"""
        return 'ensemble' in [m.lower() for m in models]

    def _get_scenario_multiplier(self, scenario: str) -> float:
        """Get multiplier for scenario adjustment"""
        multipliers = {
            'optimistic': 1.1,  # 10% increase
            'pessimistic': 0.9,  # 10% decrease
            'realistic': 1.0    # No change
        }
        return multipliers.get(scenario.lower(), 1.0)

    def _generate_sma_forecast(

        self,

        df: pd.DataFrame,

        days: int,

        include_confidence: bool = True

    ) -> ForecastResult:
        """Simple Moving Average forecast"""
        try:
            if len(df) < 7:
                raise ValueError("Insufficient data for SMA")

            window = min(7, len(df))
            sma_value = df['price'].rolling(window=window).mean().iloc[-1]

            if pd.isna(sma_value):
                sma_value = df['price'].mean()

            values = [float(sma_value)] * days

            # Simple confidence interval
            std_dev = df['price'].std()
            confidence_lower = [v - std_dev * 0.5 for v in values] if include_confidence else None
            confidence_upper = [v + std_dev * 0.5 for v in values] if include_confidence else None

            return ForecastResult(
                values=values,
                confidence_lower=confidence_lower,
                confidence_upper=confidence_upper,
                model_name="SMA"
            )

        except Exception as e:
            self.logger.error(f"SMA forecast failed: {str(e)}")
            raise

    def _generate_wma_forecast(

        self,

        df: pd.DataFrame,

        days: int,

        include_confidence: bool = True

    ) -> ForecastResult:
        """Weighted Moving Average forecast"""
        try:
            if len(df) < 7:
                raise ValueError("Insufficient data for WMA")

            window = min(7, len(df))
            weights = np.arange(1, window + 1)
            weights = weights / weights.sum()

            wma_value = (df['price'].tail(window) * weights).sum()

            if pd.isna(wma_value):
                wma_value = df['price'].mean()

            values = [float(wma_value)] * days

            # Confidence interval
            std_dev = df['price'].std()
            confidence_lower = [v - std_dev * 0.3 for v in values] if include_confidence else None
            confidence_upper = [v + std_dev * 0.3 for v in values] if include_confidence else None

            return ForecastResult(
                values=values,
                confidence_lower=confidence_lower,
                confidence_upper=confidence_upper,
                model_name="WMA"
            )

        except Exception as e:
            self.logger.error(f"WMA forecast failed: {str(e)}")
            raise

    def _generate_es_forecast(

        self,

        df: pd.DataFrame,

        days: int,

        include_confidence: bool = True

    ) -> ForecastResult:
        """Exponential Smoothing forecast"""
        try:
            # Lazy import statsmodels
            _import_statsmodels()

            if not STATS_MODELS_AVAILABLE:
                raise ImportError("statsmodels not available")

            if len(df) < 7:
                raise ValueError("Insufficient data for Exponential Smoothing")

            # Prepare data for exponential smoothing
            ts_data = df.set_index('date')['price']

            model = ExponentialSmoothing(ts_data, seasonal='add', seasonal_periods=7)
            fitted_model = model.fit()

            forecast = fitted_model.forecast(days)
            values = forecast.values.tolist()

            # Get confidence intervals if available
            if include_confidence:
                try:
                    pred = fitted_model.get_prediction()
                    confidence_intervals = pred.conf_int()
                    confidence_lower = confidence_intervals.iloc[:, 0].tail(days).values.tolist()
                    confidence_upper = confidence_intervals.iloc[:, 1].tail(days).values.tolist()
                except:
                    # Fallback confidence interval
                    std_dev = df['price'].std()
                    confidence_lower = [v - std_dev for v in values]
                    confidence_upper = [v + std_dev for v in values]
            else:
                confidence_lower = None
                confidence_upper = None

            return ForecastResult(
                values=values,
                confidence_lower=confidence_lower,
                confidence_upper=confidence_upper,
                model_name="ES"
            )

        except Exception as e:
            self.logger.error(f"ES forecast failed: {str(e)}")
            raise

    def _generate_arima_forecast(

        self,

        df: pd.DataFrame,

        days: int,

        include_confidence: bool = True

    ) -> ForecastResult:
        """ARIMA forecast"""
        try:
            # Lazy import statsmodels
            _import_statsmodels()

            if not STATS_MODELS_AVAILABLE:
                raise ImportError("statsmodels not available")

            if len(df) < 10:
                raise ValueError("Insufficient data for ARIMA")

            # Prepare data
            ts_data = df.set_index('date')['price']

            model = ARIMA(ts_data, order=(5, 1, 0))
            fitted_model = model.fit()

            forecast = fitted_model.forecast(days)
            values = forecast.values.tolist()

            # Get confidence intervals
            if include_confidence:
                try:
                    pred = fitted_model.get_forecast(days)
                    confidence_intervals = pred.conf_int()
                    confidence_lower = confidence_intervals.iloc[:, 0].values.tolist()
                    confidence_upper = confidence_intervals.iloc[:, 1].values.tolist()
                except:
                    # Fallback confidence interval
                    std_dev = df['price'].std()
                    confidence_lower = [v - std_dev for v in values]
                    confidence_upper = [v + std_dev for v in values]
            else:
                confidence_lower = None
                confidence_upper = None

            return ForecastResult(
                values=values,
                confidence_lower=confidence_lower,
                confidence_upper=confidence_upper,
                model_name="ARIMA"
            )

        except Exception as e:
            self.logger.error(f"ARIMA forecast failed: {str(e)}")
            raise

    def _generate_catboost_forecast(

        self,

        df: pd.DataFrame,

        days: int,

        include_confidence: bool = True

    ) -> ForecastResult:
        """CatBoost forecast using trained model"""
        try:
            # Lazy import CatBoost
            _import_catboost()

            if not CATBOOST_AVAILABLE:
                raise ImportError("CatBoost not available")

            if len(df) < 10:
                raise ValueError("Insufficient data for CatBoost")

            # Try to load the trained model
            model_loaded = _load_catboost_model()
            
            if model_loaded and _catboost_model is not None:
                # Use trained model for prediction
                self.logger.info("Using trained CatBoost model")
                values = self._predict_with_catboost(df, days)
            else:
                # Fallback to trend-based forecast if model not available
                self.logger.info("Using CatBoost trend-based fallback (no trained model)")
                values = self._catboost_trend_fallback(df, days)

            # Calculate confidence intervals based on historical volatility
            std_dev = df['price'].std()
            volatility_factor = std_dev / df['price'].mean() if df['price'].mean() > 0 else 0.1
            
            confidence_lower = None
            confidence_upper = None
            if include_confidence:
                # Widen confidence intervals over time
                confidence_lower = []
                confidence_upper = []
                for i, v in enumerate(values):
                    width = std_dev * (1 + 0.1 * i)  # Increasing uncertainty
                    confidence_lower.append(max(0, v - width))
                    confidence_upper.append(v + width)

            return ForecastResult(
                values=values,
                confidence_lower=confidence_lower,
                confidence_upper=confidence_upper,
                model_name="CatBoost",
                metrics=_catboost_metrics if _catboost_metrics else {}
            )

        except Exception as e:
            self.logger.error(f"CatBoost forecast failed: {str(e)}")
            raise
    
    def _predict_with_catboost(self, df: pd.DataFrame, days: int) -> List[float]:
        """Generate predictions using trained CatBoost model"""
        try:
            values = []
            last_date = df['date'].max()
            last_price = df['price'].iloc[-1]
            
            # Create a simple feature set for prediction
            # Since we don't have all the features the model was trained on,
            # we'll use the available data and fill in defaults
            for i in range(days):
                forecast_date = last_date + timedelta(days=i+1)
                
                # Build features similar to training data
                features = {
                    'year': forecast_date.year,
                    'month': forecast_date.month,
                    'day': forecast_date.day,
                    'day_of_week': forecast_date.weekday(),
                    'week_of_year': forecast_date.isocalendar()[1],
                    'quarter': (forecast_date.month - 1) // 3 + 1,
                    'is_weekend': 1 if forecast_date.weekday() >= 5 else 0,
                    'is_holiday': 0,  # Simplified
                }
                
                # Add lag features from historical data
                if len(df) > 0:
                    features['price_lag_1'] = df['price'].iloc[-1] if len(df) >= 1 else last_price
                    features['price_lag_7'] = df['price'].iloc[-7] if len(df) >= 7 else last_price
                    features['price_lag_30'] = df['price'].iloc[-30] if len(df) >= 30 else last_price
                    features['quantity_sold_lag_1'] = df['quantity'].iloc[-1] if 'quantity' in df.columns and len(df) >= 1 else 100
                    features['quantity_sold_lag_7'] = df['quantity'].iloc[-7] if 'quantity' in df.columns and len(df) >= 7 else 100
                    features['quantity_sold_lag_30'] = df['quantity'].iloc[-30] if 'quantity' in df.columns and len(df) >= 30 else 100
                    
                    # Rolling statistics
                    features['market_price'] = df['price'].mean()
                    features['supply_index'] = 100  # Default
                    features['demand_index'] = 100  # Default
                
                # Create DataFrame for prediction
                feature_df = pd.DataFrame([features])
                
                # Fill missing features with defaults
                for feat in _catboost_feature_names:
                    if feat not in feature_df.columns:
                        feature_df[feat] = 0
                
                # Reorder columns to match training
                feature_df = feature_df.reindex(columns=_catboost_feature_names, fill_value=0)
                
                # Make prediction
                try:
                    pred = _catboost_model.predict(feature_df)[0]
                    values.append(float(pred))
                except Exception as e:
                    self.logger.warning(f"CatBoost prediction failed for day {i+1}: {e}")
                    values.append(last_price)
            
            return values
            
        except Exception as e:
            self.logger.error(f"CatBoost prediction failed: {e}")
            return self._catboost_trend_fallback(df, days)
    
    def _catboost_trend_fallback(self, df: pd.DataFrame, days: int) -> List[float]:
        """Fallback trend-based forecast for CatBoost"""
        recent_trend = df['price'].pct_change().mean()
        if pd.isna(recent_trend):
            recent_trend = 0
        last_price = df['price'].iloc[-1]

        values = []
        for i in range(days):
            trend_factor = 1 + (recent_trend * (i + 1) / days)
            predicted_price = last_price * trend_factor
            values.append(float(predicted_price))
        
        return values

    def _generate_fallback_forecast(self, df: pd.DataFrame, days: int) -> ForecastResult:
        """Fallback forecast using simple average"""
        try:
            avg_price = df['price'].mean()
            values = [float(avg_price)] * days

            # Wide confidence intervals for fallback
            std_dev = df['price'].std() if len(df) > 1 else avg_price * 0.1
            confidence_lower = [v - std_dev * 2 for v in values]
            confidence_upper = [v + std_dev * 2 for v in values]

            return ForecastResult(
                values=values,
                confidence_lower=confidence_lower,
                confidence_upper=confidence_upper,
                model_name="Fallback"
            )

        except Exception as e:
            self.logger.error(f"Fallback forecast failed: {str(e)}")
            # Ultimate fallback
            return ForecastResult(
                values=[100.0] * days,
                confidence_lower=[80.0] * days,
                confidence_upper=[120.0] * days,
                model_name="Fallback"
            )

    def _generate_ensemble_forecast(

        self,

        model_results: Dict[str, ForecastResult],

        days: int,

        include_confidence: bool = True

    ) -> ForecastResult:
        """Generate simple average ensemble forecast from multiple models (deprecated, use weighted)"""
        return self._generate_weighted_ensemble_forecast(model_results, days, include_confidence)
    
    def _generate_weighted_ensemble_forecast(

        self,

        model_results: Dict[str, ForecastResult],

        days: int,

        include_confidence: bool = True

    ) -> ForecastResult:
        """Generate weighted ensemble forecast based on model accuracy"""
        try:
            if not model_results:
                raise ValueError("No model results available for ensemble")

            # Collect valid predictions with weights
            valid_predictions = []
            weights = []
            
            for model_name, result in model_results.items():
                if model_name.lower() == 'ensemble':
                    continue
                if len(result.values) >= days:
                    valid_predictions.append(result.values[:days])
                    # Get weight from calculated weights or use default
                    weight = self.model_weights.get(model_name, 1.0 / len(model_results))
                    weights.append(weight)
            
            if not valid_predictions:
                raise ValueError("No valid predictions for ensemble")
            
            # Normalize weights
            total_weight = sum(weights)
            if total_weight > 0:
                weights = [w / total_weight for w in weights]
            else:
                weights = [1.0 / len(weights)] * len(weights)

            # Calculate weighted ensemble predictions
            ensemble_values = []
            for i in range(days):
                weighted_sum = sum(
                    values[i] * weight 
                    for values, weight in zip(valid_predictions, weights)
                )
                ensemble_values.append(weighted_sum)

            # Calculate weighted confidence intervals if needed
            confidence_bounds = None
            if include_confidence:
                confidence_bounds = self._calculate_weighted_ensemble_confidence(
                    model_results, ensemble_values, days, weights
                )

            # Aggregate metrics from component models
            ensemble_metrics = self._aggregate_ensemble_metrics(model_results)

            return ForecastResult(
                values=ensemble_values,
                confidence_lower=confidence_bounds[0] if confidence_bounds else None,
                confidence_upper=confidence_bounds[1] if confidence_bounds else None,
                model_name="Ensemble",
                metrics=ensemble_metrics
            )

        except Exception as e:
            self.logger.error(f"Ensemble forecast failed: {str(e)}")
            raise

    def _collect_valid_predictions(self, model_results: Dict[str, ForecastResult], days: int) -> List[List[float]]:
        """Collect valid predictions from all models"""
        valid_predictions = []
        for result in model_results.values():
            if len(result.values) >= days:
                valid_predictions.append(result.values[:days])
        return valid_predictions

    def _calculate_ensemble_values(self, valid_predictions: List[List[float]], days: int) -> List[float]:
        """Calculate ensemble values by averaging predictions"""
        ensemble_values = []
        for i in range(days):
            day_predictions = [values[i] for values in valid_predictions if i < len(values)]
            ensemble_values.append(np.mean(day_predictions))
        return ensemble_values

    def _calculate_ensemble_confidence(

        self,

        model_results: Dict[str, ForecastResult],

        ensemble_values: List[float],

        days: int

    ) -> tuple:
        """Calculate ensemble confidence intervals"""
        all_lower = self._collect_confidence_bounds(model_results, 'confidence_lower', days)
        all_upper = self._collect_confidence_bounds(model_results, 'confidence_upper', days)

        if all_lower and all_upper:
            confidence_lower = [np.mean([lower[i] for lower in all_lower]) for i in range(days)]
            confidence_upper = [np.mean([upper[i] for upper in all_upper]) for i in range(days)]
        else:
            # Fallback confidence intervals based on standard deviation
            std_dev = np.std(ensemble_values) if len(ensemble_values) > 1 else np.mean(ensemble_values) * 0.1
            confidence_lower = [v - std_dev for v in ensemble_values]
            confidence_upper = [v + std_dev for v in ensemble_values]

        return confidence_lower, confidence_upper
    
    def _calculate_weighted_ensemble_confidence(

        self,

        model_results: Dict[str, ForecastResult],

        ensemble_values: List[float],

        days: int,

        weights: List[float]

    ) -> Tuple[List[float], List[float]]:
        """Calculate weighted confidence intervals for ensemble"""
        all_lower = []
        all_upper = []
        model_weights_list = []
        
        for model_name, result in model_results.items():
            if model_name.lower() == 'ensemble':
                continue
            if result.confidence_lower and len(result.confidence_lower) >= days:
                all_lower.append(result.confidence_lower[:days])
                all_upper.append(result.confidence_upper[:days])
                model_weights_list.append(self.model_weights.get(model_name, 1.0))
        
        if all_lower and all_upper:
            # Normalize weights
            total_weight = sum(model_weights_list)
            if total_weight > 0:
                model_weights_list = [w / total_weight for w in model_weights_list]
            
            confidence_lower = []
            confidence_upper = []
            for i in range(days):
                lower_weighted = sum(
                    lower[i] * w for lower, w in zip(all_lower, model_weights_list)
                )
                upper_weighted = sum(
                    upper[i] * w for upper, w in zip(all_upper, model_weights_list)
                )
                confidence_lower.append(lower_weighted)
                confidence_upper.append(upper_weighted)
        else:
            # Fallback
            std_dev = np.std(ensemble_values) if len(ensemble_values) > 1 else np.mean(ensemble_values) * 0.1
            confidence_lower = [v - std_dev for v in ensemble_values]
            confidence_upper = [v + std_dev for v in ensemble_values]
        
        return confidence_lower, confidence_upper
    
    def _aggregate_ensemble_metrics(self, model_results: Dict[str, ForecastResult]) -> Dict[str, Optional[float]]:
        """Aggregate metrics from component models for ensemble"""
        aggregated = {
            'mae': [], 'rmse': [], 'mape': [], 'bias': [], 'mase': [], 'r_squared': []
        }
        
        for model_name, result in model_results.items():
            if model_name.lower() == 'ensemble':
                continue
            metrics = self.model_metrics.get(model_name, {})
            for key in aggregated:
                if metrics.get(key) is not None:
                    aggregated[key].append(metrics[key])
        
        # Calculate weighted averages
        final_metrics = {}
        for key, values in aggregated.items():
            if values:
                final_metrics[key] = round(np.mean(values), 4)
            else:
                final_metrics[key] = None
        
        final_metrics['component_models'] = len(model_results) - 1  # Exclude ensemble itself
        return final_metrics

        return confidence_lower, confidence_upper

    def _collect_confidence_bounds(

        self,

        model_results: Dict[str, ForecastResult],

        bound_type: str,

        days: int

    ) -> List[List[float]]:
        """Collect confidence bounds from all models"""
        bounds = []
        for result in model_results.values():
            bound_values = getattr(result, bound_type)
            if bound_values and len(bound_values) >= days:
                bounds.append(bound_values[:days])
        return bounds

    def _prepare_forecast_data(

        self,

        model_results: Dict[str, ForecastResult],

        df: pd.DataFrame,

        days: int

    ) -> List[Dict[str, Any]]:
        """Prepare final forecast data for API response"""
        try:
            last_date = df['date'].max()

            forecast_data = []
            for i in range(days):
                forecast_date = last_date + timedelta(days=i+1)

                # Use ensemble if available, otherwise use first available model
                if 'Ensemble' in model_results:
                    result = model_results['Ensemble']
                else:
                    result = next(iter(model_results.values()))

                data_point = {
                    "date": forecast_date.isoformat(),
                    "predicted_value": round(result.values[i], 2),
                    "model_used": result.model_name
                }

                if result.confidence_lower and i < len(result.confidence_lower):
                    data_point["confidence_lower"] = round(result.confidence_lower[i], 2)

                if result.confidence_upper and i < len(result.confidence_upper):
                    data_point["confidence_upper"] = round(result.confidence_upper[i], 2)

                forecast_data.append(data_point)

            return forecast_data

        except Exception as e:
            self.logger.error(f"Forecast data preparation failed: {str(e)}")
            raise

    def calculate_revenue_projection(

        self,

        forecast_data: List[Dict[str, Any]],

        selling_price: float,

        historical_data: pd.DataFrame

    ) -> List[Dict[str, Any]]:
        """Calculate revenue projections"""
        try:
            # Use average quantity from historical data
            avg_quantity = historical_data['quantity'].mean()

            revenue_projection = []
            for point in forecast_data:
                projected_quantity = avg_quantity
                projected_revenue = projected_quantity * selling_price

                projection = {
                    "date": point["date"],
                    "projected_quantity": round(float(projected_quantity), 2),
                    "selling_price": round(float(selling_price), 2),
                    "projected_revenue": round(float(projected_revenue), 2)
                }

                # Add confidence intervals if available
                if "confidence_lower" in point:
                    projection["confidence_lower"] = round(point["confidence_lower"] * projected_quantity, 2)
                if "confidence_upper" in point:
                    projection["confidence_upper"] = round(point["confidence_upper"] * projected_quantity, 2)

                revenue_projection.append(projection)

            return revenue_projection

        except Exception as e:
            self.logger.error(f"Revenue projection calculation failed: {str(e)}")
            return []

    def generate_summary(

        self,

        forecast_data: List[Dict[str, Any]],

        historical_data: pd.DataFrame,

        models_used: List[str],

        scenario: str

    ) -> str:
        """Generate AI-like summary of forecast results"""
        try:
            # Calculate key metrics
            metrics = self._calculate_forecast_metrics(forecast_data, historical_data)

            # Generate summary sections
            overview = self._generate_overview_section(metrics, forecast_data, scenario)
            key_metrics = self._generate_metrics_section(metrics, forecast_data, models_used)
            analysis = self._generate_analysis_section()
            recommendations = self._generate_recommendations_section(metrics['trend'])

            return f"""# Price Forecast Summary



{overview}



{key_metrics}



{analysis}



{recommendations}"""

        except Exception as e:
            self.logger.error(f"Summary generation failed: {str(e)}")
            return "Forecast summary generation failed."

    def _calculate_forecast_metrics(

        self,

        forecast_data: List[Dict[str, Any]],

        historical_data: pd.DataFrame

    ) -> Dict[str, Any]:
        """Calculate key metrics for the forecast"""
        forecast_values = [point["predicted_value"] for point in forecast_data]
        avg_forecast = np.mean(forecast_values)
        avg_historical = historical_data['price'].mean()

        trend = "increasing" if avg_forecast > avg_historical else "decreasing"
        change_percent = abs((avg_forecast - avg_historical) / avg_historical * 100)

        return {
            'avg_forecast': avg_forecast,
            'avg_historical': avg_historical,
            'trend': trend,
            'change_percent': change_percent
        }

    def _generate_overview_section(

        self,

        metrics: Dict[str, Any],

        forecast_data: List[Dict[str, Any]],

        scenario: str

    ) -> str:
        """Generate the overview section of the summary"""
        return f"""## Overview

Based on historical demand data, the forecast shows a **{metrics['trend']}** trend over the next {len(forecast_data)} days using {scenario} scenario."""

    def _generate_metrics_section(

        self,

        metrics: Dict[str, Any],

        forecast_data: List[Dict[str, Any]],

        models_used: List[str]

    ) -> str:
        """Generate the key metrics section"""
        return f"""## Key Metrics

- **Average Historical Price**: ${metrics['avg_historical']:.2f}

- **Average Forecasted Price**: ${metrics['avg_forecast']:.2f}

- **Expected Change**: {metrics['change_percent']:.1f}% {metrics['trend']}

- **Models Used**: {', '.join(models_used)}

- **Forecast Horizon**: {len(forecast_data)} days"""

    def _generate_analysis_section(self) -> str:
        """Generate the analysis section"""
        return """## Analysis

The forecast combines multiple statistical and machine learning models to provide reliable predictions. Confidence intervals are included to help assess prediction uncertainty."""

    def _generate_recommendations_section(self, trend: str) -> str:
        """Generate the recommendations section"""
        if trend == 'increasing':
            recommendation = "Consider increasing inventory to meet potential higher demand."
        else:
            recommendation = "Monitor market conditions closely as prices may decline."

        return f"""## Recommendations

{recommendation}

Track actual prices against this forecast and adjust strategies accordingly."""

    def calculate_overall_confidence(self, forecast_data: List[Dict[str, Any]]) -> Optional[float]:
        """Calculate overall confidence score"""
        try:
            confidence_scores = []

            for point in forecast_data:
                if "confidence_lower" in point and "confidence_upper" in point:
                    lower = point["confidence_lower"]
                    upper = point["confidence_upper"]
                    predicted = point["predicted_value"]

                    # Calculate confidence interval width relative to prediction
                    if predicted != 0:
                        interval_width = (upper - lower) / predicted
                        # Convert to confidence score (0-100)
                        confidence = max(0, min(100, 100 - (interval_width * 50)))
                        confidence_scores.append(confidence)

            if confidence_scores:
                return round(np.mean(confidence_scores), 1)

            return None

        except Exception as e:
            self.logger.error(f"Confidence calculation failed: {str(e)}")
            return None