# """
# AI-driven smart suggestions for inventory management
# Generates top 3 recommendations based on demand forecasting
# """
# import pandas as pd
# import numpy as np
# from datetime import datetime, timedelta
# from typing import List, Dict
# import warnings
# warnings.filterwarnings('ignore')

# def get_smart_suggestions(orders_df: pd.DataFrame, inventory_df: pd.DataFrame = None, top_n: int = 3) -> List[Dict]:
#     """
#     Generate AI-driven inventory recommendations
    
#     Args:
#         orders_df: DataFrame with columns [order_date, sku, qty]
#         inventory_df: Optional DataFrame with current inventory levels
#         top_n: Number of recommendations to return
    
#     Returns:
#         List of recommendation dictionaries
#     """
#     recommendations = []
    
#     # Ensure order_date is datetime
#     orders_df['order_date'] = pd.to_datetime(orders_df['order_date'])
    
#     # Get SKU-level insights
#     sku_stats = analyze_sku_patterns(orders_df)
    
#     # 1. HIGH DEMAND ALERT - SKUs with increasing demand
#     high_demand_skus = identify_high_demand(sku_stats, orders_df)
#     for sku_info in high_demand_skus[:2]:
#         recommendations.append({
#             'type': 'high_demand',
#             'priority': 'high',
#             'sku': sku_info['sku'],
#             'message': f"📈 High demand detected for {sku_info['sku'][:20]}...",
#             'detail': f"Recent orders up {sku_info['increase']:.0f}% vs previous period. Consider increasing stock.",
#             'confidence': sku_info['confidence']
#         })
    
#     # 2. RESTOCK ALERT - SKUs running low based on demand velocity
#     restock_skus = identify_restock_needs(sku_stats, orders_df)
#     for sku_info in restock_skus[:2]:
#         recommendations.append({
#             'type': 'restock',
#             'priority': 'medium',
#             'sku': sku_info['sku'],
#             'message': f"⚠️ Restock recommended for {sku_info['sku'][:20]}...",
#             'detail': f"Avg {sku_info['avg_daily']:.1f} units/day. Forecast: {sku_info['forecast']:.0f} units next 7 days.",
#             'confidence': sku_info['confidence']
#         })
    
#     # 3. SLOW MOVING - SKUs with declining demand
#     slow_moving = identify_slow_moving(sku_stats, orders_df)
#     for sku_info in slow_moving[:1]:
#         recommendations.append({
#             'type': 'slow_moving',
#             'priority': 'low',
#             'sku': sku_info['sku'],
#             'message': f"📉 Declining demand for {sku_info['sku'][:20]}...",
#             'detail': f"Orders down {sku_info['decline']:.0f}% recently. Consider promotional activities.",
#             'confidence': sku_info['confidence']
#         })
    
#     # Sort by priority and return top N
#     priority_order = {'high': 0, 'medium': 1, 'low': 2}
#     recommendations.sort(key=lambda x: priority_order[x['priority']])
    
#     return recommendations[:top_n]


# def analyze_sku_patterns(orders_df: pd.DataFrame) -> pd.DataFrame:
#     """Analyze order patterns for each SKU"""
#     sku_stats = orders_df.groupby('sku').agg({
#         'qty': ['sum', 'mean', 'count'],
#         'order_date': ['min', 'max']
#     }).reset_index()
    
#     sku_stats.columns = ['sku', 'total_qty', 'avg_qty', 'order_count', 'first_order', 'last_order']
    
#     # Calculate days active
#     sku_stats['days_active'] = (sku_stats['last_order'] - sku_stats['first_order']).dt.days + 1
#     sku_stats['avg_daily_qty'] = sku_stats['total_qty'] / sku_stats['days_active']
    
#     return sku_stats


# def identify_high_demand(sku_stats: pd.DataFrame, orders_df: pd.DataFrame) -> List[Dict]:
#     """Identify SKUs with increasing demand trends"""
#     results = []
#     cutoff_date = orders_df['order_date'].max() - timedelta(days=30)
    
#     for _, row in sku_stats.iterrows():
#         sku = row['sku']
#         sku_orders = orders_df[orders_df['sku'] == sku].copy()
        
#         if len(sku_orders) < 5:
#             continue
        
#         # Recent vs historical comparison
#         recent = sku_orders[sku_orders['order_date'] > cutoff_date]['qty'].sum()
#         historical = sku_orders[sku_orders['order_date'] <= cutoff_date]['qty'].sum()
        
#         if historical > 0:
#             # Normalize by time period
#             recent_days = (sku_orders['order_date'].max() - cutoff_date).days
#             historical_days = (cutoff_date - sku_orders['order_date'].min()).days
            
#             if recent_days > 0 and historical_days > 0:
#                 recent_daily = recent / recent_days
#                 historical_daily = historical / historical_days
                
#                 if historical_daily > 0:
#                     increase = ((recent_daily - historical_daily) / historical_daily) * 100
                    
#                     if increase > 20:  # 20% increase threshold
#                         results.append({
#                             'sku': sku,
#                             'increase': increase,
#                             'confidence': min(95, 70 + (increase / 10))
#                         })
    
#     return sorted(results, key=lambda x: x['increase'], reverse=True)


# def identify_restock_needs(sku_stats: pd.DataFrame, orders_df: pd.DataFrame) -> List[Dict]:
#     """Identify SKUs that need restocking based on velocity"""
#     results = []
    
#     for _, row in sku_stats.nlargest(20, 'avg_daily_qty').iterrows():
#         sku = row['sku']
#         avg_daily = row['avg_daily_qty']
        
#         # Simple 7-day forecast
#         forecast_7d = avg_daily * 7
        
#         if avg_daily > 0.5:  # Only for items with reasonable velocity
#             results.append({
#                 'sku': sku,
#                 'avg_daily': avg_daily,
#                 'forecast': forecast_7d,
#                 'confidence': min(90, 60 + (row['order_count'] * 2))
#             })
    
#     return sorted(results, key=lambda x: x['forecast'], reverse=True)


# def identify_slow_moving(sku_stats: pd.DataFrame, orders_df: pd.DataFrame) -> List[Dict]:
#     """Identify SKUs with declining demand"""
#     results = []
#     cutoff_date = orders_df['order_date'].max() - timedelta(days=30)
    
#     for _, row in sku_stats.iterrows():
#         sku = row['sku']
#         sku_orders = orders_df[orders_df['sku'] == sku].copy()
        
#         if len(sku_orders) < 5:
#             continue
        
#         recent = sku_orders[sku_orders['order_date'] > cutoff_date]['qty'].sum()
#         historical = sku_orders[sku_orders['order_date'] <= cutoff_date]['qty'].sum()
        
#         if recent > 0 and historical > 0:
#             recent_days = (sku_orders['order_date'].max() - cutoff_date).days
#             historical_days = (cutoff_date - sku_orders['order_date'].min()).days
            
#             if recent_days > 0 and historical_days > 0:
#                 recent_daily = recent / recent_days
#                 historical_daily = historical / historical_days
                
#                 if historical_daily > 0:
#                     decline = ((historical_daily - recent_daily) / historical_daily) * 100
                    
#                     if decline > 30:  # 30% decline threshold
#                         results.append({
#                             'sku': sku,
#                             'decline': decline,
#                             'confidence': min(85, 60 + (decline / 5))
#                         })
    
#     return sorted(results, key=lambda x: x['decline'], reverse=True)