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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
# Chart Generator MCP Service
"""
Enterprise Chart Payload Generator

Generates frontend-compatible chart payloads for:
- Forecast line charts with confidence bands
- Scenario comparison bar charts
- Churn distribution charts
- Risk heatmaps
- Driver waterfall charts
- Multi-dataset overlay charts

Usage:
    from mcp.chart_generator import ChartGenerator
    generator = ChartGenerator()
    payload = generator.forecast_chart(prediction_result)
"""

from typing import Dict, List, Any, Optional
from dataclasses import dataclass, asdict
from datetime import datetime
import json


@dataclass
class ChartPayload:
    """Base chart payload for frontend rendering"""
    chart_type: str
    title: str
    x: List[str]
    series: List[Dict[str, Any]]
    options: Dict[str, Any]
    

class ChartGenerator:
    """
    Enterprise Chart Payload Generator.
    
    Generates structured payloads compatible with:
    - Recharts (React)
    - Chart.js
    - Custom chart components
    """
    
    # Chart type constants
    FORECAST_LINE = 'forecast_line'
    SCENARIO_BAR = 'scenario_bar'
    CHURN_DIST = 'churn_distribution'
    RISK_HEATMAP = 'risk_heatmap'
    DRIVER_WATERFALL = 'driver_waterfall'
    COMPARISON_OVERLAY = 'comparison_overlay'
    PROFIT_LOSS = 'profit_loss_curve'
    
    def __init__(self):
        self.default_colors = {
            'primary': '#f97316',      # Orange
            'secondary': '#3b82f6',    # Blue
            'success': '#22c55e',      # Green
            'danger': '#ef4444',       # Red
            'warning': '#eab308',      # Yellow
            'muted': '#6b7280',        # Gray
            'confidence': 'rgba(249, 115, 22, 0.2)',  # Orange transparent
        }
        
    def forecast_chart(
        self,
        historical: List[Dict],
        forecast: List[Dict],
        title: str = "Forecast",
        currency: str = "₹",
        show_confidence: bool = True
    ) -> Dict[str, Any]:
        """
        Generate forecast line chart with confidence bands.
        
        Args:
            historical: List of {date, value} for historical data
            forecast: List of {date, value, lower, upper} for forecast
            title: Chart title
            currency: Currency symbol
            show_confidence: Whether to show confidence bands
            
        Returns:
            Chart payload for frontend
        """
        # Build x-axis labels
        x_labels = []
        for h in historical:
            date = h.get('date', '')
            if hasattr(date, 'strftime'):
                x_labels.append(date.strftime('%b %Y'))
            else:
                x_labels.append(str(date)[:10])
                
        for f in forecast:
            date = f.get('date', '')
            if hasattr(date, 'strftime'):
                x_labels.append(date.strftime('%b %Y'))
            else:
                x_labels.append(str(date)[:10])
        
        # Build series data
        n_historical = len(historical)
        n_total = n_historical + len(forecast)
        
        # Historical series (actual values)
        y_actual = [h.get('value') for h in historical] + [None] * len(forecast)
        
        # Forecast series (with connection to last historical point)
        y_forecast = [None] * (n_historical - 1)
        if historical:
            y_forecast.append(historical[-1].get('value'))  # Connect point
        y_forecast.extend([f.get('value') for f in forecast])
        
        # Confidence bands
        y_upper = [None] * n_historical
        y_lower = [None] * n_historical
        
        if show_confidence:
            for f in forecast:
                y_upper.append(f.get('upper', f.get('value') * 1.1))
                y_lower.append(f.get('lower', f.get('value') * 0.9))
        
        return {
            'chart_type': self.FORECAST_LINE,
            'title': title,
            'x': x_labels,
            'y_actual': y_actual,
            'y_forecast': y_forecast,
            'y_upper': y_upper if show_confidence else [],
            'y_lower': y_lower if show_confidence else [],
            'confidence_band': show_confidence,
            'currency': currency,
            'series': [
                {
                    'name': 'Historical',
                    'data': y_actual,
                    'type': 'line',
                    'color': self.default_colors['secondary'],
                    'strokeWidth': 3,
                    'dot': True
                },
                {
                    'name': 'Forecast',
                    'data': y_forecast,
                    'type': 'line',
                    'color': self.default_colors['primary'],
                    'strokeWidth': 3,
                    'strokeDasharray': '8 4',
                    'dot': True
                }
            ],
            'options': {
                'animationDuration': 1000,
                'showGrid': True,
                'showLegend': True,
                'yAxisFormatter': f'{currency}{{value}}',
                'tooltipFormatter': f'{currency}{{value:,.0f}}'
            }
        }
    
    def scenario_chart(
        self,
        scenarios: List[Dict],
        metric: str = 'profit',
        title: str = "Scenario Comparison",
        currency: str = "₹",
        best_scenario: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        Generate scenario comparison bar chart.
        
        Args:
            scenarios: List of scenario dicts with name, revenue, profit, risk
            metric: Which metric to compare ('revenue', 'profit')
            title: Chart title
            currency: Currency symbol
            best_scenario: Name of recommended scenario
            
        Returns:
            Chart payload for frontend
        """
        x_labels = [s.get('name', f'Scenario {i+1}') for i, s in enumerate(scenarios)]
        values = [s.get(metric, 0) for s in scenarios]
        changes = [s.get('change_pct', 0) for s in scenarios]
        risks = [s.get('risk', 'medium') for s in scenarios]
        
        # Determine bar colors
        colors = []
        for i, s in enumerate(scenarios):
            name = s.get('name', '')
            if name == best_scenario:
                colors.append(self.default_colors['success'])
            elif name == 'Current State':
                colors.append(self.default_colors['secondary'])
            elif s.get('risk') == 'high':
                colors.append(self.default_colors['danger'])
            else:
                colors.append(self.default_colors['primary'])
        
        return {
            'chart_type': self.SCENARIO_BAR,
            'title': title,
            'x': x_labels,
            'values': values,
            'changes': changes,
            'risks': risks,
            'colors': colors,
            'best_scenario': best_scenario,
            'currency': currency,
            'series': [
                {
                    'name': metric.capitalize(),
                    'data': values,
                    'type': 'bar',
                    'colors': colors
                }
            ],
            'options': {
                'animationDuration': 800,
                'barRadius': 8,
                'showLabels': True,
                'labelPosition': 'top',
                'highlightBest': True
            }
        }
    
    def churn_chart(
        self,
        churn_data: List[Dict],
        title: str = "Churn Prediction"
    ) -> Dict[str, Any]:
        """
        Generate churn probability distribution chart.
        
        Args:
            churn_data: List of {segment, probability, count}
            title: Chart title
            
        Returns:
            Chart payload for frontend
        """
        segments = [d.get('segment', f'Segment {i+1}') for i, d in enumerate(churn_data)]
        probabilities = [d.get('probability', 0) for d in churn_data]
        counts = [d.get('count', 0) for d in churn_data]
        
        # Color by risk level
        colors = []
        for prob in probabilities:
            if prob > 0.3:
                colors.append(self.default_colors['danger'])
            elif prob > 0.15:
                colors.append(self.default_colors['warning'])
            else:
                colors.append(self.default_colors['success'])
        
        return {
            'chart_type': self.CHURN_DIST,
            'title': title,
            'x': segments,
            'probabilities': probabilities,
            'counts': counts,
            'colors': colors,
            'series': [
                {
                    'name': 'Churn Risk',
                    'data': [p * 100 for p in probabilities],  # Convert to percentage
                    'type': 'bar',
                    'colors': colors
                },
                {
                    'name': 'Customer Count',
                    'data': counts,
                    'type': 'line',
                    'color': self.default_colors['muted'],
                    'yAxisId': 'right'
                }
            ],
            'options': {
                'showDualAxis': True,
                'leftAxisLabel': 'Churn Risk (%)',
                'rightAxisLabel': 'Customer Count',
                'highlightHighRisk': True
            }
        }
    
    def profit_loss_chart(
        self,
        data: List[Dict],
        title: str = "Profit/Loss Analysis",
        currency: str = "₹"
    ) -> Dict[str, Any]:
        """
        Generate profit vs loss curve chart.
        
        Args:
            data: List of {period, revenue, cost, profit}
            title: Chart title
            currency: Currency symbol
            
        Returns:
            Chart payload for frontend
        """
        periods = [d.get('period', f'P{i+1}') for i, d in enumerate(data)]
        revenues = [d.get('revenue', 0) for d in data]
        costs = [d.get('cost', 0) for d in data]
        profits = [d.get('profit', r - c) for d, r, c in zip(data, revenues, costs)]
        
        # Calculate cumulative profit
        cumulative = []
        total = 0
        for p in profits:
            total += p
            cumulative.append(total)
        
        # Determine profit/loss colors for each point
        profit_colors = [
            self.default_colors['success'] if p >= 0 else self.default_colors['danger']
            for p in profits
        ]
        
        return {
            'chart_type': self.PROFIT_LOSS,
            'title': title,
            'x': periods,
            'revenues': revenues,
            'costs': costs,
            'profits': profits,
            'cumulative': cumulative,
            'currency': currency,
            'series': [
                {
                    'name': 'Revenue',
                    'data': revenues,
                    'type': 'area',
                    'color': self.default_colors['secondary'],
                    'fillOpacity': 0.3
                },
                {
                    'name': 'Cost',
                    'data': costs,
                    'type': 'area',
                    'color': self.default_colors['danger'],
                    'fillOpacity': 0.3
                },
                {
                    'name': 'Profit',
                    'data': profits,
                    'type': 'bar',
                    'colors': profit_colors
                }
            ],
            'options': {
                'showBreakeven': True,
                'breakEvenLine': 0,
                'highlightProfit': True
            }
        }
    
    def comparison_overlay_chart(
        self,
        datasets: List[Dict],
        title: str = "Multi-Dataset Comparison"
    ) -> Dict[str, Any]:
        """
        Generate multi-dataset overlay chart.
        
        Args:
            datasets: List of {name, data, color} where data is [{x, y}]
            title: Chart title
            
        Returns:
            Chart payload for frontend
        """
        # Find all unique x values
        all_x = set()
        for ds in datasets:
            for point in ds.get('data', []):
                all_x.add(point.get('x', ''))
        x_labels = sorted(list(all_x))
        
        # Build series
        series = []
        colors = [
            self.default_colors['primary'],
            self.default_colors['secondary'],
            self.default_colors['success'],
            self.default_colors['warning'],
            self.default_colors['danger']
        ]
        
        for i, ds in enumerate(datasets):
            name = ds.get('name', f'Dataset {i+1}')
            color = ds.get('color', colors[i % len(colors)])
            data_dict = {p.get('x'): p.get('y') for p in ds.get('data', [])}
            values = [data_dict.get(x) for x in x_labels]
            
            series.append({
                'name': name,
                'data': values,
                'type': 'line',
                'color': color,
                'strokeWidth': 2
            })
        
        return {
            'chart_type': self.COMPARISON_OVERLAY,
            'title': title,
            'x': x_labels,
            'series': series,
            'options': {
                'showLegend': True,
                'legendPosition': 'top',
                'enableHover': True,
                'showDots': True
            }
        }
    
    def driver_waterfall_chart(
        self,
        drivers: List[Dict],
        title: str = "Driver Analysis",
        currency: str = "₹"
    ) -> Dict[str, Any]:
        """
        Generate driver waterfall chart (what caused the change).
        
        Args:
            drivers: List of {name, impact, type} where type is 'increase' or 'decrease'
            title: Chart title
            currency: Currency symbol
            
        Returns:
            Chart payload for frontend
        """
        names = [d.get('name', f'Driver {i+1}') for i, d in enumerate(drivers)]
        impacts = [d.get('impact', 0) for d in drivers]
        types = [d.get('type', 'increase' if d.get('impact', 0) >= 0 else 'decrease') 
                 for d in drivers]
        
        # Calculate running total for waterfall
        running = [0]
        for impact in impacts:
            running.append(running[-1] + impact)
        
        colors = [
            self.default_colors['success'] if t == 'increase' else self.default_colors['danger']
            for t in types
        ]
        
        return {
            'chart_type': self.DRIVER_WATERFALL,
            'title': title,
            'x': names,
            'impacts': impacts,
            'running_total': running[1:],
            'types': types,
            'colors': colors,
            'currency': currency,
            'series': [
                {
                    'name': 'Impact',
                    'data': impacts,
                    'type': 'waterfall',
                    'colors': colors
                }
            ],
            'options': {
                'showConnectors': True,
                'showTotalBar': True,
                'startLabel': 'Start',
                'endLabel': 'End'
            }
        }
    
    def risk_heatmap(
        self,
        risk_matrix: List[List[float]],
        x_labels: List[str],
        y_labels: List[str],
        title: str = "Risk Assessment"
    ) -> Dict[str, Any]:
        """
        Generate risk heatmap.
        
        Args:
            risk_matrix: 2D array of risk scores (0-1)
            x_labels: Column labels
            y_labels: Row labels
            title: Chart title
            
        Returns:
            Chart payload for frontend
        """
        # Flatten matrix to points
        points = []
        for i, row in enumerate(risk_matrix):
            for j, val in enumerate(row):
                # Determine color based on risk level
                if val > 0.7:
                    color = self.default_colors['danger']
                elif val > 0.4:
                    color = self.default_colors['warning']
                else:
                    color = self.default_colors['success']
                    
                points.append({
                    'x': j,
                    'y': i,
                    'value': val,
                    'color': color,
                    'label': f'{val:.1%}'
                })
        
        return {
            'chart_type': self.RISK_HEATMAP,
            'title': title,
            'x_labels': x_labels,
            'y_labels': y_labels,
            'points': points,
            'matrix': risk_matrix,
            'options': {
                'showLabels': True,
                'colorScale': {
                    'low': self.default_colors['success'],
                    'medium': self.default_colors['warning'],
                    'high': self.default_colors['danger']
                },
                'cellRadius': 4
            }
        }


# Convenience functions
def generate_forecast_chart(prediction_result: Dict) -> Dict[str, Any]:
    """Generate forecast chart from prediction result."""
    generator = ChartGenerator()
    return generator.forecast_chart(
        historical=prediction_result.get('historical_points', []),
        forecast=prediction_result.get('forecast_points', []),
        title=prediction_result.get('title') or f"Revenue Forecast ({len(prediction_result.get('forecast_points', []))} Periods)"
    )


def generate_scenario_chart(simulation_result: Dict) -> Dict[str, Any]:
    """Generate scenario chart from simulation result."""
    generator = ChartGenerator()
    return generator.scenario_chart(
        scenarios=simulation_result.get('scenarios', []),
        best_scenario=simulation_result.get('best_scenario')
    )


# Quick test
if __name__ == "__main__":
    generator = ChartGenerator()
    
    # Test forecast chart
    historical = [
        {'date': '2024-01', 'value': 10000},
        {'date': '2024-02', 'value': 10500},
        {'date': '2024-03', 'value': 11000},
    ]
    forecast = [
        {'date': '2024-04', 'value': 11500, 'lower': 10500, 'upper': 12500},
        {'date': '2024-05', 'value': 12000, 'lower': 10800, 'upper': 13200},
    ]
    
    chart = generator.forecast_chart(historical, forecast, "Test Forecast")
    print("Forecast Chart:")
    print(f"  Type: {chart['chart_type']}")
    print(f"  X Labels: {chart['x']}")
    print(f"  Series: {len(chart['series'])}")