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
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
"""
Advanced API v5 - Cutting Edge Features

Integrates all advanced features:
- AI Sentiment Analysis
- Pattern Recognition
- Smart Bankroll
- AI Assistant
- Live Betting
"""

from flask import Blueprint, jsonify, request
from datetime import datetime
from typing import Dict, List

# Import advanced modules
from src.ai_sentiment import (
    analyze_match_sentiment, get_smart_advice,
    sentiment_analyzer, market_tracker
)
from src.pattern_recognition import (
    detect_patterns, check_anomalies, predict_exact_score,
    pattern_engine, anomaly_detector
)
from src.smart_bankroll import (
    calculate_optimal_stake, get_bankroll_status,
    record_bet_result, default_bankroll
)
from src.ai_assistant import chat, get_chat_history, ai_assistant
from src.live_predictor import (
    register_live_match, update_live_match,
    get_live_prediction, get_all_live_predictions,
    find_live_value_bets, live_betting_manager
)


# Create blueprint
advanced_api = Blueprint('advanced_api', __name__, url_prefix='/api/v5')


# ============================================================
# AI Sentiment Analysis Endpoints
# ============================================================

@advanced_api.route('/sentiment/match')
def match_sentiment():
    """Get sentiment analysis for a match"""
    home = request.args.get('home', '')
    away = request.args.get('away', '')
    
    if not home or not away:
        return jsonify({'success': False, 'error': 'Missing home or away team'})
    
    # For demo, use sample news
    sample_news = [
        f"{home} looking confident ahead of big match",
        f"{home} key players fit and available",
        f"Manager praises {home} form in training",
        f"{away} struggling with injuries",
        f"Questions over {away} morale after poor results",
        f"{away} fans concerned about team performance"
    ]
    
    sentiment = analyze_match_sentiment(home, away, sample_news)
    
    return jsonify({
        'success': True,
        'sentiment': sentiment,
        'news_analyzed': len(sample_news)
    })


@advanced_api.route('/sentiment/advice')
def smart_advice():
    """Get AI-powered betting advice"""
    home = request.args.get('home', '')
    away = request.args.get('away', '')
    
    if not home or not away:
        return jsonify({'success': False, 'error': 'Missing teams'})
    
    try:
        from src.enhanced_predictor_v2 import enhanced_predict
        
        prediction = enhanced_predict(home, away, 'bundesliga')
        advice = get_smart_advice(
            {'home': home, 'away': away},
            prediction
        )
        
        return jsonify({
            'success': True,
            'advice': advice
        })
    except Exception as e:
        return jsonify({'success': False, 'error': str(e)})


# ============================================================
# Pattern Recognition Endpoints
# ============================================================

@advanced_api.route('/patterns/<team>')
def team_patterns(team: str):
    """Get detected patterns for a team"""
    patterns = detect_patterns(team)
    
    return jsonify({
        'success': True,
        'team': team,
        'patterns': patterns,
        'count': len(patterns)
    })


@advanced_api.route('/patterns/anomalies')
def check_anomalies_endpoint():
    """Check for anomalies in odds"""
    match_id = request.args.get('match_id', '')
    home_odds = float(request.args.get('home', 2.0))
    draw_odds = float(request.args.get('draw', 3.5))
    away_odds = float(request.args.get('away', 3.0))
    
    odds = {'home': home_odds, 'draw': draw_odds, 'away': away_odds}
    anomaly = check_anomalies(match_id, odds)
    
    return jsonify({
        'success': True,
        'match_id': match_id,
        'anomaly': anomaly,
        'has_anomaly': anomaly is not None
    })


@advanced_api.route('/patterns/score')
def exact_score():
    """Predict exact score"""
    home = request.args.get('home', '')
    away = request.args.get('away', '')
    
    if not home or not away:
        return jsonify({'success': False, 'error': 'Missing teams'})
    
    prediction = predict_exact_score(home, away)
    
    return jsonify({
        'success': True,
        'prediction': prediction
    })


# ============================================================
# Smart Bankroll Endpoints
# ============================================================

@advanced_api.route('/bankroll/status')
def bankroll_status():
    """Get bankroll status"""
    status = get_bankroll_status()
    return jsonify({
        'success': True,
        'status': status
    })


@advanced_api.route('/bankroll/stake')
def calculate_stake():
    """Calculate optimal stake for a bet"""
    probability = float(request.args.get('probability', 0.5))
    odds = float(request.args.get('odds', 2.0))
    bankroll = float(request.args.get('bankroll', 0)) or None
    risk_level = request.args.get('risk', 'moderate')
    
    recommendation = calculate_optimal_stake(probability, odds, bankroll, risk_level)
    
    return jsonify({
        'success': True,
        'recommendation': recommendation
    })


@advanced_api.route('/bankroll/record', methods=['POST'])
def record_result():
    """Record a bet result"""
    data = request.get_json() or {}
    stake = float(data.get('stake', 10))
    odds = float(data.get('odds', 2.0))
    won = data.get('won', False)
    
    record_bet_result(stake, odds, won)
    
    return jsonify({
        'success': True,
        'new_status': get_bankroll_status()
    })


@advanced_api.route('/bankroll/portfolio', methods=['POST'])
def optimize_portfolio():
    """Optimize stake allocation across multiple bets"""
    data = request.get_json() or {}
    bets = data.get('bets', [])
    
    if not bets:
        return jsonify({'success': False, 'error': 'No bets provided'})
    
    allocation = default_bankroll.get_portfolio_allocation(bets)
    
    return jsonify({
        'success': True,
        'allocation': allocation,
        'total_stake': sum(b['recommended_stake'] for b in allocation)
    })


# ============================================================
# AI Assistant Endpoints
# ============================================================

@advanced_api.route('/chat', methods=['POST'])
def chat_endpoint():
    """Chat with AI betting assistant"""
    data = request.get_json() or {}
    message = data.get('message', '')
    user_id = data.get('user_id', 'default')
    
    if not message:
        return jsonify({'success': False, 'error': 'No message provided'})
    
    response = chat(message, user_id)
    
    return jsonify({
        'success': True,
        'response': response
    })


@advanced_api.route('/chat/history')
def chat_history():
    """Get chat history"""
    user_id = request.args.get('user_id', 'default')
    limit = int(request.args.get('limit', 10))
    
    history = get_chat_history(user_id, limit)
    
    return jsonify({
        'success': True,
        'history': history
    })


# ============================================================
# Live Betting Endpoints
# ============================================================

@advanced_api.route('/live/register', methods=['POST'])
def register_match():
    """Register a match for live tracking"""
    data = request.get_json() or {}
    home = data.get('home', '')
    away = data.get('away', '')
    
    if not home or not away:
        return jsonify({'success': False, 'error': 'Missing teams'})
    
    match_id = register_live_match(home, away)
    
    return jsonify({
        'success': True,
        'match_id': match_id
    })


@advanced_api.route('/live/update', methods=['POST'])
def update_match():
    """Update live match state"""
    data = request.get_json() or {}
    match_id = data.get('match_id', '')
    updates = data.get('updates', {})
    
    if not match_id:
        return jsonify({'success': False, 'error': 'Missing match_id'})
    
    prediction = update_live_match(match_id, **updates)
    
    if prediction:
        return jsonify({
            'success': True,
            'prediction': prediction
        })
    else:
        return jsonify({'success': False, 'error': 'Match not found'})


@advanced_api.route('/live/prediction/<match_id>')
def live_prediction(match_id: str):
    """Get live prediction for a match"""
    prediction = get_live_prediction(match_id)
    
    if prediction:
        return jsonify({
            'success': True,
            **prediction
        })
    else:
        return jsonify({'success': False, 'error': 'Match not found'})


@advanced_api.route('/live/all')
def all_live():
    """Get all live matches with predictions"""
    matches = get_all_live_predictions()
    
    return jsonify({
        'success': True,
        'matches': matches,
        'count': len(matches)
    })


@advanced_api.route('/live/value')
def live_value_bets():
    """Find live value betting opportunities"""
    min_edge = float(request.args.get('min_edge', 0.1))
    
    opportunities = find_live_value_bets(min_edge)
    
    return jsonify({
        'success': True,
        'opportunities': opportunities,
        'count': len(opportunities)
    })


# ============================================================
# Combined Analysis Endpoints
# ============================================================

@advanced_api.route('/analyze')
def full_analysis():
    """Get complete analysis for a match"""
    home = request.args.get('home', '')
    away = request.args.get('away', '')
    
    if not home or not away:
        return jsonify({'success': False, 'error': 'Missing teams'})
    
    try:
        from src.enhanced_predictor_v2 import enhanced_predict_with_goals
        
        # Get prediction
        prediction = enhanced_predict_with_goals(home, away, 'bundesliga')
        
        # Get patterns
        home_patterns = detect_patterns(home)
        away_patterns = detect_patterns(away)
        
        # Get score prediction
        score = predict_exact_score(home, away)
        
        # Get sentiment
        sentiment = analyze_match_sentiment(home, away)
        
        # Get advice
        advice = get_smart_advice({'home': home, 'away': away}, prediction)
        
        # Calculate stake
        stake = calculate_optimal_stake(
            prediction.get('confidence', 0.5),
            prediction.get('odds', {}).get('home', 2.0)
        )
        
        return jsonify({
            'success': True,
            'analysis': {
                'match': f"{home} vs {away}",
                'prediction': prediction,
                'patterns': {
                    'home': home_patterns,
                    'away': away_patterns
                },
                'exact_score': score,
                'sentiment': sentiment,
                'advice': advice,
                'stake_recommendation': stake,
                'generated_at': datetime.now().isoformat()
            }
        })
        
    except Exception as e:
        return jsonify({'success': False, 'error': str(e)})


# ============================================================
# Health & Info
# ============================================================

@advanced_api.route('/health')
def health():
    """API health check"""
    return jsonify({
        'success': True,
        'status': 'healthy',
        'version': 'v5',
        'features': [
            'ai_sentiment_analysis',
            'pattern_recognition',
            'anomaly_detection',
            'smart_bankroll',
            'kelly_criterion',
            'ai_assistant',
            'live_betting',
            'momentum_analysis',
            'exact_score_prediction',
            'portfolio_optimization'
        ],
        'timestamp': datetime.now().isoformat()
    })


@advanced_api.route('/info')
def info():
    """API information"""
    return jsonify({
        'success': True,
        'api': {
            'name': 'FootyPredict Pro Advanced API',
            'version': 'v5.0.0',
            'base_url': '/api/v5',
            'categories': {
                'sentiment': ['/sentiment/match', '/sentiment/advice'],
                'patterns': ['/patterns/<team>', '/patterns/anomalies', '/patterns/score'],
                'bankroll': ['/bankroll/status', '/bankroll/stake', '/bankroll/portfolio'],
                'assistant': ['/chat', '/chat/history'],
                'live': ['/live/register', '/live/update', '/live/prediction', '/live/value'],
                'analysis': ['/analyze']
            }
        }
    })


def register_advanced_api(app):
    """Register the advanced API blueprint"""
    app.register_blueprint(advanced_api)
    print("🚀 Advanced API v5 registered at /api/v5")