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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
"""Response formatting utilities"""

from typing import Any, Dict, List, Optional, Tuple
import plotly.graph_objects as go
import plotly.express as px


def format_card_display(card_data: Dict) -> str:
    """Format card recommendation for display"""
    card_name = card_data.get("card_name", "Unknown Card")
    reward_rate = card_data.get("reward_rate", 0)
    reward_amount = card_data.get("reward_amount", 0)
    category = card_data.get("category", "General")
    reasoning = card_data.get("reasoning", "")
    
    return f"""
### ๐Ÿ’ณ {card_name}

**Reward Rate:** {reward_rate}x points  
**Reward Amount:** ${reward_amount:.2f}  
**Category:** {category}  
**Why:** {reasoning}
"""

def format_full_recommendation(response: Dict) -> str:
    """Format complete recommendation response"""
    if response.get("error"):
        return f"โŒ **Error:** {response.get('message', 'Unknown error')}"
    
    # Header
    output = f"""
# ๐ŸŽฏ Recommendation for {response.get('merchant', 'Unknown')}

**Amount:** ${response.get('amount_usd', 0):.2f}  
**Date:** {response.get('transaction_date', 'N/A')}  
**User:** {response.get('user_id', 'N/A')}

---

## ๐Ÿ† Best Card to Use

"""
    
    # Recommended card
    recommended = response.get("recommended_card", {})
    output += format_card_display(recommended)
    
    # RAG Insights
    rag_insights = response.get("rag_insights")
    if rag_insights:
        output += f"""
---

## ๐Ÿ“š Card Benefits

{rag_insights.get('benefits', 'No additional information available.')}
"""
        if rag_insights.get('tips'):
            output += f"""
๐Ÿ’ก **Pro Tip:** {rag_insights.get('tips')}
"""
    
    # Forecast Warning
    forecast = response.get("forecast_warning")
    if forecast:
        risk_level = forecast.get("risk_level", "low")
        message = forecast.get("message", "")
        
        if risk_level == "high":
            emoji = "๐Ÿšจ"
        elif risk_level == "medium":
            emoji = "โš ๏ธ"
        else:
            emoji = "โœ…"
        
        output += f"""
---

## {emoji} Spending Status

{message}

**Current Spend:** ${forecast.get('current_spend', 0):.2f}  
**Spending Cap:** ${forecast.get('cap', 0):.2f}  
**Projected Spend:** ${forecast.get('projected_spend', 0):.2f}
"""
    
    # Alternative cards
    alternatives = response.get("alternative_cards", [])
    if alternatives:
        output += "\n---\n\n## ๐Ÿ”„ Alternative Cards\n\n"
        for i, alt in enumerate(alternatives[:2], 1):
            output += f"### Option {i}\n"
            output += format_card_display(alt)
    
    # Metadata
    services = response.get("services_used", [])
    time_ms = response.get("orchestration_time_ms", 0)
    
    output += f"""
---

**Services Used:** {', '.join(services)}  
**Response Time:** {time_ms:.0f}ms
"""
    
    return output

def format_comparison_table(cards: list) -> str:
    """Format card comparison as markdown table"""
    if not cards:
        return "No cards to compare."
    
    table = """
| Card | Reward Rate | Reward Amount | Category |
|------|-------------|---------------|----------|
"""
    
    for card in cards:
        name = card.get("card_name", "Unknown")
        rate = card.get("reward_rate", 0)
        amount = card.get("reward_amount", 0)
        category = card.get("category", "N/A")
        table += f"| {name} | {rate}x | ${amount:.2f} | {category} |\n"
    
    return table

# utils/formatters.py

def format_analytics_metrics(analytics: Dict[str, Any]) -> tuple:
    """
    Format analytics data for display
    
    Returns:
        Tuple of (metrics_html, table_md, insights_md, forecast_md)
    """
    # Format metric cards
    metrics_html = f"""
    <div style="display: flex; gap: 10px; flex-wrap: wrap;">
        <div class="metric-card" style="flex: 1; min-width: 200px;">
            <h2>${analytics['annual_savings']}</h2>
            <p>๐Ÿ’ฐ Potential Annual Savings</p>
        </div>
        <div class="metric-card metric-card-green" style="flex: 1; min-width: 200px;">
            <h2>{analytics['rate_increase']}%</h2>
            <p>๐Ÿ“ˆ Rewards Rate Increase</p>
        </div>
        <div class="metric-card metric-card-orange" style="flex: 1; min-width: 200px;">
            <h2>{analytics['optimized_transactions']}</h2>
            <p>โœ… Optimized Transactions</p>
        </div>
        <div class="metric-card metric-card-blue" style="flex: 1; min-width: 200px;">
            <h2>{analytics['optimization_score']}/100</h2>
            <p>โญ Optimization Score</p>
        </div>
    </div>
    """
    
    # Format spending breakdown table
    table_rows = []
    for cat in analytics['category_breakdown']:
        table_rows.append(
            f"| {cat['category']} | ${cat['monthly_spend']:.2f} | {cat['best_card']} | "
            f"${cat['rewards']:.2f} | {cat['rate']} |"
        )
    
    table_md = f"""
| Category | Monthly Spend | Best Card | Rewards | Rate |
|----------|---------------|-----------|---------|------|
{chr(10).join(table_rows)}
| **Total** | **${analytics['total_monthly_spend']:.2f}** | - | **${analytics['total_monthly_rewards']:.2f}** | **{analytics['average_rate']}%** |
    """
    
    # Format insights
    top_cats = "\n".join([
        f"{i+1}. {cat['name']}: ${cat['amount']:.2f} ({cat['change']})"
        for i, cat in enumerate(analytics['top_categories'])
    ])
    
    insights_md = f"""
**๐Ÿ”ฅ Top Spending Categories:**
{top_cats}

**๐Ÿ’ก Optimization Opportunities:**
- โœ… You're using optimal cards {analytics['optimization_score']}% of the time
- ๐ŸŽฏ Switch to Chase Freedom for Q4 5% grocery bonus
- โš ๏ธ Amex Gold dining cap approaching ($2,000 limit)
- ๐Ÿ’ณ Consider applying for Citi Custom Cash

**๐Ÿ† Best Performing Card:**
{analytics['category_breakdown'][0]['best_card']} - ${analytics['category_breakdown'][0]['rewards']:.2f} rewards earned

**๐Ÿ“Š Year-to-Date:**
- Total Rewards: ${analytics['ytd_rewards']:.2f}
- Potential if optimized: ${analytics['ytd_potential']:.2f}
- **Money left on table: ${analytics['money_left']:.2f}**
    """
    
    # Format forecast
    forecast = analytics['forecast']
    recommendations = "\n".join([f"{i+1}. {rec}" for i, rec in enumerate(analytics['recommendations'])])
    
    forecast_md = f"""
### ๐Ÿ”ฎ Next Month Forecast

Based on your spending patterns:
- **Predicted Spend:** ${forecast['next_month_spend']:.2f}
- **Predicted Rewards:** ${forecast['next_month_rewards']:.2f}
- **Cards to Watch:** {', '.join(forecast['cards_to_watch'])}

**Recommendations:**
{recommendations}
    """
    
    return metrics_html, table_md, insights_md, forecast_md

def create_spending_chart(analytics: Dict[str, Any]) -> go.Figure:
    """
    Create a bar chart showing spending by category
    
    Args:
        analytics: Analytics data dictionary
        
    Returns:
        Plotly figure object
    """
    categories = [cat['category'] for cat in analytics['category_breakdown']]
    spending = [cat['monthly_spend'] for cat in analytics['category_breakdown']]
    rewards = [cat['rewards'] for cat in analytics['category_breakdown']]
    
    fig = go.Figure()
    
    # Add spending bars
    fig.add_trace(go.Bar(
        name='Monthly Spend',
        x=categories,
        y=spending,
        marker_color='rgb(102, 126, 234)',
        text=[f'${s:.0f}' for s in spending],
        textposition='outside',
    ))
    
    # Add rewards bars
    fig.add_trace(go.Bar(
        name='Rewards Earned',
        x=categories,
        y=rewards,
        marker_color='rgb(56, 239, 125)',
        text=[f'${r:.2f}' for r in rewards],
        textposition='outside',
    ))
    
    fig.update_layout(
        title='๐Ÿ’ฐ Spending vs Rewards by Category',
        xaxis_title='Category',
        yaxis_title='Amount ($)',
        barmode='group',
        height=400,
        template='plotly_white',
        showlegend=True,
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=1.02,
            xanchor="right",
            x=1
        )
    )
    
    return fig


def create_rewards_pie_chart(analytics: Dict[str, Any]) -> go.Figure:
    """
    Create a pie chart showing rewards distribution by category
    
    Args:
        analytics: Analytics data dictionary
        
    Returns:
        Plotly figure object
    """
    categories = [cat['category'] for cat in analytics['category_breakdown']]
    rewards = [cat['rewards'] for cat in analytics['category_breakdown']]
    
    fig = go.Figure(data=[go.Pie(
        labels=categories,
        values=rewards,
        hole=0.4,  # Donut chart
        marker=dict(
            colors=['#667eea', '#764ba2', '#f093fb', '#f5576c', '#4facfe', '#00f2fe']
        ),
        textinfo='label+percent',
        textposition='outside',
        hovertemplate='<b>%{label}</b><br>Rewards: $%{value:.2f}<br>%{percent}<extra></extra>'
    )])
    
    fig.update_layout(
        title='๐ŸŽฏ Rewards Distribution by Category',
        height=400,
        template='plotly_white',
        showlegend=True,
        legend=dict(
            orientation="v",
            yanchor="middle",
            y=0.5,
            xanchor="left",
            x=1.05
        )
    )
    
    return fig


def create_optimization_gauge(analytics: Dict[str, Any]) -> go.Figure:
    """
    Create a gauge chart showing optimization score
    
    Args:
        analytics: Analytics data dictionary
        
    Returns:
        Plotly figure object
    """
    score = analytics['optimization_score']
    
    fig = go.Figure(go.Indicator(
        mode="gauge+number+delta",
        value=score,
        domain={'x': [0, 1], 'y': [0, 1]},
        title={'text': "โญ Optimization Score", 'font': {'size': 24}},
        delta={'reference': 80, 'increasing': {'color': "green"}},
        gauge={
            'axis': {'range': [None, 100], 'tickwidth': 1, 'tickcolor': "darkblue"},
            'bar': {'color': "darkblue"},
            'bgcolor': "white",
            'borderwidth': 2,
            'bordercolor': "gray",
            'steps': [
                {'range': [0, 50], 'color': '#ffcccb'},
                {'range': [50, 75], 'color': '#ffffcc'},
                {'range': [75, 100], 'color': '#ccffcc'}
            ],
            'threshold': {
                'line': {'color': "red", 'width': 4},
                'thickness': 0.75,
                'value': 90
            }
        }
    ))
    
    fig.update_layout(
        height=300,
        template='plotly_white',
        margin=dict(l=20, r=20, t=60, b=20)
    )
    
    return fig


def create_trend_line_chart(analytics: Dict[str, Any]) -> go.Figure:
    """
    Create a line chart showing spending trends (mock data for demo)
    
    Args:
        analytics: Analytics data dictionary
        
    Returns:
        Plotly figure object
    """
    import random
    
    # Generate mock monthly data
    months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    
    # Base spending with some variance
    base_spend = analytics['total_monthly_spend']
    spending_data = [base_spend * (0.8 + random.random() * 0.4) for _ in months]
    
    # Calculate rewards based on average rate
    avg_rate = analytics['average_rate'] / 100
    rewards_data = [spend * avg_rate for spend in spending_data]
    
    fig = go.Figure()
    
    # Add spending line
    fig.add_trace(go.Scatter(
        x=months,
        y=spending_data,
        mode='lines+markers',
        name='Monthly Spending',
        line=dict(color='rgb(102, 126, 234)', width=3),
        marker=dict(size=8),
        hovertemplate='<b>%{x}</b><br>Spending: $%{y:.2f}<extra></extra>'
    ))
    
    # Add rewards line
    fig.add_trace(go.Scatter(
        x=months,
        y=rewards_data,
        mode='lines+markers',
        name='Rewards Earned',
        line=dict(color='rgb(56, 239, 125)', width=3),
        marker=dict(size=8),
        hovertemplate='<b>%{x}</b><br>Rewards: $%{y:.2f}<extra></extra>'
    ))
    
    fig.update_layout(
        title='๐Ÿ“ˆ 12-Month Spending & Rewards Trend',
        xaxis_title='Month',
        yaxis_title='Amount ($)',
        height=400,
        template='plotly_white',
        hovermode='x unified',
        showlegend=True,
        legend=dict(
            orientation="h",
            yanchor="bottom",
            y=1.02,
            xanchor="right",
            x=1
        )
    )
    
    return fig


def create_card_performance_chart(analytics: Dict[str, Any]) -> go.Figure:
    """
    Create a horizontal bar chart showing card performance
    
    Args:
        analytics: Analytics data dictionary
        
    Returns:
        Plotly figure object
    """
    # Aggregate rewards by card
    card_rewards = {}
    for cat in analytics['category_breakdown']:
        card = cat['best_card']
        reward = cat['rewards']
        if card in card_rewards:
            card_rewards[card] += reward
        else:
            card_rewards[card] = reward
    
    # Sort by rewards
    sorted_cards = sorted(card_rewards.items(), key=lambda x: x[0], reverse=True)
    cards = [card for card, _ in sorted_cards]
    rewards = [reward for _, reward in sorted_cards]
    
    fig = go.Figure(go.Bar(
        x=rewards,
        y=cards,
        orientation='h',
        marker=dict(
            color=rewards,
            colorscale='Viridis',
            showscale=True,
            colorbar=dict(title="Rewards ($)")
        ),
        text=[f'${r:.2f}' for r in rewards],
        textposition='outside',
        hovertemplate='<b>%{y}</b><br>Total Rewards: $%{x:.2f}<extra></extra>'
    ))
    
    fig.update_layout(
        title='๐Ÿ† Card Performance Ranking',
        xaxis_title='Total Rewards Earned ($)',
        yaxis_title='Credit Card',
        height=400,
        template='plotly_white',
        margin=dict(l=150)
    )
    
    return fig