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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
"""
🎯 PRO ANALYST ENGINE v2.0 - DataVision Intelligence
=====================================================

A world-class intelligent data analyst that understands ANY data.

Features:
- 🧠 Smart Query Intent Detection
- πŸ“Š Auto Statistics & Insights
- 🎯 Dynamic Chart Selection
- πŸ“ˆ Trend & Anomaly Detection
- πŸ”— Multi-RAG Strategy Selection
- πŸ’‘ Natural Language Insights

Built for DataVision - Not just business data, ANY data!

Author: DataVision Team
Version: 2.0.0
"""

import logging
import pandas as pd
import numpy as np
from typing import Dict, Any, List, Optional, Tuple
from datetime import datetime
from enum import Enum
from dataclasses import dataclass
import re
import json

from services.vector_store import vector_store

logger = logging.getLogger(__name__)

# LLM for intelligent responses
try:
    from core.llm import chat as llm_chat
    LLM_AVAILABLE = True
except ImportError:
    LLM_AVAILABLE = False

# RAG Router
try:
    from core.rag_router import route_query, RAGStrategy
    RAG_AVAILABLE = True
except ImportError:
    RAG_AVAILABLE = False

# Universal Visualizer
try:
    from core.mode_engines.universal_visualizer import UniversalVisualizer
    VISUALIZER_AVAILABLE = True
except ImportError:
    VISUALIZER_AVAILABLE = False

# Smart Visualization MCP
try:
    from mcp.smart_visualization import smart_visualize, SmartVisualization
    SMART_VIZ_AVAILABLE = True
except ImportError:
    SMART_VIZ_AVAILABLE = False

# Advanced Hybrid Intelligence System
try:
    from core.knowledge_sources import (
        KnowledgeSource, SourceClassifier, HybridResponseCombiner,
        SOURCE_BADGES, classify_query, get_source_badge
    )
    HYBRID_KNOWLEDGE_AVAILABLE = True
except ImportError:
    HYBRID_KNOWLEDGE_AVAILABLE = False

try:
    from core.advanced_rag import AdaptiveRAG, RAGType
    ADVANCED_RAG_AVAILABLE = True
except ImportError:
    ADVANCED_RAG_AVAILABLE = False

try:
    from core.deep_agents import HybridAgent, deep_agent_query
    DEEP_AGENTS_AVAILABLE = True
except ImportError:
    DEEP_AGENTS_AVAILABLE = False

# Intelligent Visualizer (Knowledge Graphs, Mind Maps, 20+ Charts)
try:
    from core.intelligent_visualizer import (
        IntelligentVisualizer, VizType, smart_visualize,
        generate_knowledge_graph, generate_mind_map
    )
    INTELLIGENT_VIZ_AVAILABLE = True
except ImportError:
    INTELLIGENT_VIZ_AVAILABLE = False

# Intelligent Query Processor (Claude-style)
try:
    from core.intelligent_processor import IntelligentQueryProcessor, intelligent_process
    INTELLIGENT_PROCESSOR_AVAILABLE = True
except ImportError:
    INTELLIGENT_PROCESSOR_AVAILABLE = False


# =============================================================================
# QUERY INTENT DETECTION
# =============================================================================

class AnalystIntent(Enum):
    """Types of analysis queries"""
    SUMMARY = "summary"           # "Give me a summary"
    AGGREGATION = "aggregation"   # "Total sales", "Average price"
    COMPARISON = "comparison"     # "Compare A vs B"
    TREND = "trend"               # "Show trend over time"
    DISTRIBUTION = "distribution" # "Distribution of X"
    CORRELATION = "correlation"   # "Relationship between X and Y"
    RANKING = "ranking"           # "Top 10", "Best performing"
    FILTERING = "filtering"       # "Show where X > Y"
    OUTLIERS = "outliers"         # "Find anomalies"
    BREAKDOWN = "breakdown"       # "Sales by category"
    COUNT = "count"               # "How many"
    PERCENTAGE = "percentage"     # "What percent"
    GROWTH = "growth"             # "Growth rate"
    FORECAST = "forecast"         # "Predict next month"
    GENERAL = "general"           # General question


@dataclass
class QueryAnalysis:
    """Result of analyzing a user query"""
    intent: AnalystIntent
    confidence: float
    target_columns: List[str]
    group_by: Optional[str]
    time_column: Optional[str]
    aggregation: Optional[str]  # sum, mean, count, etc.
    filter_conditions: List[str]
    chart_suggestion: str
    wants_chart: bool = False    # User explicitly asked for chart
    wants_brief: bool = False    # User wants short answer


def detect_analyst_intent(query: str, df: pd.DataFrame = None) -> QueryAnalysis:
    """
    Intelligently detect what the user wants to analyze.
    Returns structured analysis of the query.
    """
    q = query.lower().strip()
    columns = list(df.columns) if df is not None else []
    columns_lower = [c.lower() for c in columns]
    
    # Intent patterns
    intent_patterns = {
        AnalystIntent.SUMMARY: [
            r'summar', r'overview', r'describe', r'tell me about',
            r'what.*data', r'explain.*data'
        ],
        AnalystIntent.AGGREGATION: [
            r'total', r'sum of', r'average', r'mean', r'median',
            r'minimum', r'maximum', r'count'
        ],
        AnalystIntent.COMPARISON: [
            r'compare', r'versus', r' vs ', r'difference between',
            r'higher than', r'lower than', r'better than'
        ],
        AnalystIntent.TREND: [
            r'trend', r'over time', r'by month', r'by year', r'by day',
            r'growth', r'decline', r'change over'
        ],
        AnalystIntent.DISTRIBUTION: [
            r'distribution', r'spread', r'histogram', r'frequency',
            r'how.*distributed'
        ],
        AnalystIntent.CORRELATION: [
            r'correlat', r'relationship', r'related', r'affect',
            r'impact on', r'depends on'
        ],
        AnalystIntent.RANKING: [
            r'top \d+', r'bottom \d+', r'best', r'worst', r'highest',
            r'lowest', r'rank', r'leading'
        ],
        AnalystIntent.FILTERING: [
            r'where', r'filter', r'only.*where', r'show.*where',
            r'greater than', r'less than'
        ],
        AnalystIntent.OUTLIERS: [
            r'outlier', r'anomal', r'unusual', r'extreme', r'abnormal'
        ],
        AnalystIntent.BREAKDOWN: [
            r'by category', r'by type', r'breakdown', r'per',
            r'group by', r'for each'
        ],
        AnalystIntent.COUNT: [
            r'how many', r'count of', r'number of', r'quantity'
        ],
        AnalystIntent.PERCENTAGE: [
            r'percent', r'proportion', r'share', r'ratio', r'%'
        ],
        AnalystIntent.GROWTH: [
            r'growth', r'increase', r'decrease', r'changed by'
        ],
        AnalystIntent.FORECAST: [
            r'predict', r'forecast', r'next month', r'future', r'estimate'
        ]
    }
    
    # Detect intent
    detected_intent = AnalystIntent.GENERAL
    max_confidence = 0.5
    
    for intent, patterns in intent_patterns.items():
        for pattern in patterns:
            if re.search(pattern, q):
                detected_intent = intent
                max_confidence = 0.85
                break
        if max_confidence > 0.8:
            break
    
    # Detect target columns
    target_columns = []
    for col in columns:
        if col.lower() in q or col.lower().replace('_', ' ') in q:
            target_columns.append(col)
    
    # Detect group by column
    group_by = None
    group_patterns = [r'by (\w+)', r'per (\w+)', r'for each (\w+)']
    for pattern in group_patterns:
        match = re.search(pattern, q)
        if match:
            potential_group = match.group(1)
            for col in columns:
                if potential_group in col.lower():
                    group_by = col
                    break
    
    # Detect time column
    time_column = None
    time_keywords = ['date', 'time', 'year', 'month', 'day', 'created', 'updated']
    for col in columns:
        if any(kw in col.lower() for kw in time_keywords):
            time_column = col
            break
    
    # Detect aggregation
    agg_map = {
        'total': 'sum', 'sum': 'sum', 'average': 'mean', 'mean': 'mean',
        'median': 'median', 'count': 'count', 'minimum': 'min',
        'maximum': 'max', 'min': 'min', 'max': 'max'
    }
    aggregation = None
    for word, agg in agg_map.items():
        if word in q:
            aggregation = agg
            break
    
    # Suggest chart type
    chart_map = {
        AnalystIntent.TREND: 'line',
        AnalystIntent.DISTRIBUTION: 'histogram',
        AnalystIntent.COMPARISON: 'bar',
        AnalystIntent.RANKING: 'bar',
        AnalystIntent.BREAKDOWN: 'pie',
        AnalystIntent.CORRELATION: 'scatter',
        AnalystIntent.PERCENTAGE: 'pie',
        AnalystIntent.COUNT: 'bar',
    }
    chart_suggestion = chart_map.get(detected_intent, 'bar')
    
    # Detect if user EXPLICITLY wants a chart
    chart_keywords = ['chart', 'graph', 'plot', 'visualize', 'visualization', 'show me', 'display', 'draw']
    wants_chart = any(kw in q for kw in chart_keywords)
    
    # Detect if user wants brief/short response
    brief_keywords = ['one word', 'brief', 'short', 'single word', 'just tell', 'only answer', 'yes or no', 'just say']
    wants_brief = any(kw in q for kw in brief_keywords)
    
    return QueryAnalysis(
        intent=detected_intent,
        confidence=max_confidence,
        target_columns=target_columns,
        group_by=group_by,
        time_column=time_column,
        aggregation=aggregation,
        filter_conditions=[],
        chart_suggestion=chart_suggestion,
        wants_chart=wants_chart,
        wants_brief=wants_brief
    )


# =============================================================================
# AUTO STATISTICS ENGINE
# =============================================================================

def calculate_auto_statistics(df: pd.DataFrame, analysis: QueryAnalysis) -> Dict[str, Any]:
    """
    Automatically calculate relevant statistics based on the query intent.
    """
    stats = {
        'dataset_info': {
            'rows': len(df),
            'columns': len(df.columns),
            'memory_mb': df.memory_usage(deep=True).sum() / 1024 / 1024
        },
        'computed_metrics': {}
    }
    
    # Get numeric and categorical columns
    numeric_cols = df.select_dtypes(include=[np.number]).columns.tolist()
    categorical_cols = df.select_dtypes(include=['object', 'category']).columns.tolist()
    
    # Basic statistics for numeric columns
    if numeric_cols:
        stats['numeric_summary'] = {}
        for col in numeric_cols[:10]:  # Limit to 10 columns
            try:
                stats['numeric_summary'][col] = {
                    'mean': float(df[col].mean()),
                    'median': float(df[col].median()),
                    'std': float(df[col].std()),
                    'min': float(df[col].min()),
                    'max': float(df[col].max()),
                    'missing': int(df[col].isna().sum())
                }
            except:
                pass
    
    # Intent-specific calculations
    if analysis.intent == AnalystIntent.SUMMARY:
        # Full summary
        if numeric_cols:
            total_col = numeric_cols[0]
            stats['computed_metrics']['total'] = float(df[total_col].sum())
            stats['computed_metrics']['average'] = float(df[total_col].mean())
    
    elif analysis.intent == AnalystIntent.AGGREGATION:
        # Perform requested aggregation
        if analysis.target_columns and analysis.aggregation:
            for col in analysis.target_columns:
                if col in df.columns:
                    if analysis.aggregation == 'sum':
                        stats['computed_metrics'][f'total_{col}'] = float(df[col].sum())
                    elif analysis.aggregation == 'mean':
                        stats['computed_metrics'][f'average_{col}'] = float(df[col].mean())
                    elif analysis.aggregation == 'count':
                        stats['computed_metrics'][f'count_{col}'] = int(df[col].count())
    
    elif analysis.intent == AnalystIntent.CORRELATION:
        # Correlation matrix
        if len(numeric_cols) >= 2:
            corr = df[numeric_cols[:5]].corr()
            stats['correlation_matrix'] = corr.to_dict()
    
    elif analysis.intent == AnalystIntent.OUTLIERS:
        # Detect outliers using IQR
        stats['outliers'] = {}
        for col in numeric_cols[:5]:
            Q1 = df[col].quantile(0.25)
            Q3 = df[col].quantile(0.75)
            IQR = Q3 - Q1
            outlier_count = len(df[(df[col] < Q1 - 1.5*IQR) | (df[col] > Q3 + 1.5*IQR)])
            stats['outliers'][col] = {
                'count': int(outlier_count),
                'percentage': float(outlier_count / len(df) * 100)
            }
    
    elif analysis.intent == AnalystIntent.BREAKDOWN:
        # Group by analysis
        if analysis.group_by and analysis.group_by in df.columns:
            breakdown = df.groupby(analysis.group_by).size().to_dict()
            stats['breakdown'] = {str(k): int(v) for k, v in breakdown.items()}
    
    elif analysis.intent == AnalystIntent.RANKING:
        # Top/Bottom N
        if analysis.target_columns:
            col = analysis.target_columns[0]
            if col in df.columns:
                stats['top_10'] = df.nlargest(10, col)[[col]].to_dict()
    
    return stats


# =============================================================================
# CHART GENERATOR
# =============================================================================

def generate_analyst_chart(df: pd.DataFrame, analysis: QueryAnalysis, stats: Dict) -> Optional[Dict]:
    """
    Generate appropriate Plotly chart based on analysis.
    """
    try:
        chart_type = analysis.chart_suggestion
        
        if chart_type == 'bar' and analysis.group_by:
            # Grouped bar chart
            grouped = df.groupby(analysis.group_by).size().head(10)
            return {
                "data": [{
                    "type": "bar",
                    "x": [str(x) for x in grouped.index.tolist()],
                    "y": grouped.values.tolist(),
                    "marker": {"color": "#3b82f6"}
                }],
                "layout": {
                    "title": {"text": f"Count by {analysis.group_by}", "font": {"size": 16}},
                    "xaxis": {"title": analysis.group_by},
                    "yaxis": {"title": "Count"},
                    "paper_bgcolor": "#f8fafc"
                }
            }
        
        elif chart_type == 'histogram' and analysis.target_columns:
            # Histogram
            col = analysis.target_columns[0]
            if col in df.columns:
                return {
                    "data": [{
                        "type": "histogram",
                        "x": df[col].dropna().tolist()[:1000],
                        "marker": {"color": "#8b5cf6"}
                    }],
                    "layout": {
                        "title": {"text": f"Distribution of {col}", "font": {"size": 16}},
                        "xaxis": {"title": col},
                        "yaxis": {"title": "Frequency"},
                        "paper_bgcolor": "#f8fafc"
                    }
                }
        
        elif chart_type == 'line' and analysis.time_column:
            # Time series
            numeric_cols = df.select_dtypes(include=[np.number]).columns[:3]
            if len(numeric_cols) > 0:
                time_sorted = df.sort_values(analysis.time_column).head(100)
                return {
                    "data": [{
                        "type": "scatter",
                        "mode": "lines+markers",
                        "x": time_sorted[analysis.time_column].astype(str).tolist(),
                        "y": time_sorted[numeric_cols[0]].tolist(),
                        "name": numeric_cols[0],
                        "line": {"color": "#10b981"}
                    }],
                    "layout": {
                        "title": {"text": f"{numeric_cols[0]} Over Time", "font": {"size": 16}},
                        "xaxis": {"title": "Time"},
                        "yaxis": {"title": numeric_cols[0]},
                        "paper_bgcolor": "#f8fafc"
                    }
                }
        
        elif chart_type == 'pie' and analysis.group_by:
            # Pie chart
            grouped = df.groupby(analysis.group_by).size().head(8)
            return {
                "data": [{
                    "type": "pie",
                    "labels": [str(x) for x in grouped.index.tolist()],
                    "values": grouped.values.tolist(),
                    "hole": 0.4
                }],
                "layout": {
                    "title": {"text": f"Distribution by {analysis.group_by}", "font": {"size": 16}},
                    "paper_bgcolor": "#f8fafc"
                }
            }
        
        elif chart_type == 'scatter' and len(analysis.target_columns) >= 2:
            # Scatter plot
            col1, col2 = analysis.target_columns[:2]
            if col1 in df.columns and col2 in df.columns:
                sample = df[[col1, col2]].dropna().head(500)
                return {
                    "data": [{
                        "type": "scatter",
                        "mode": "markers",
                        "x": sample[col1].tolist(),
                        "y": sample[col2].tolist(),
                        "marker": {"color": "#ef4444", "opacity": 0.6}
                    }],
                    "layout": {
                        "title": {"text": f"{col1} vs {col2}", "font": {"size": 16}},
                        "xaxis": {"title": col1},
                        "yaxis": {"title": col2},
                        "paper_bgcolor": "#f8fafc"
                    }
                }
        
        # Default: Overview bar chart of numeric means
        numeric_cols = df.select_dtypes(include=[np.number]).columns[:8]
        if len(numeric_cols) > 0:
            means = df[numeric_cols].mean()
            return {
                "data": [{
                    "type": "bar",
                    "x": [str(c)[:15] for c in means.index.tolist()],
                    "y": means.values.tolist(),
                    "marker": {"color": "#6366f1"}
                }],
                "layout": {
                    "title": {"text": "Average Values by Column", "font": {"size": 16}},
                    "xaxis": {"tickangle": -45},
                    "yaxis": {"title": "Mean Value"},
                    "paper_bgcolor": "#f8fafc"
                }
            }
        
        return None
        
    except Exception as e:
        logger.error(f"Chart generation error: {e}")
        return None


# =============================================================================
# PRO ANALYST ENGINE CLASS
# =============================================================================

class ProAnalystEngine:
    """
    🎯 PRO ANALYST ENGINE - DataVision Intelligence
    
    The smartest data analyst that understands ANY data type.
    
    πŸ’‘ WHEN TO USE:
    - Quick data analysis questions
    - Statistics and aggregations
    - Data exploration
    - Simple visualizations
    
    Features:
    - Smart query intent detection
    - Auto statistics calculation
    - Dynamic chart generation
    - Natural language insights
    """
    
    def __init__(self, user_id: str):
        self.user_id = user_id
        self.analysis_history = []
    
    def process(
        self,
        query: str,
        context: str = "",
        df: pd.DataFrame = None,
        generate_chart: bool = True
    ) -> Dict[str, Any]:
        """
        Process a data analysis query with full intelligence.
        Uses DYNAMIC routing with visualizations for both data AND AI knowledge.
        """
        result = {
            "answer": "",
            "mode": "analyst",
            "confidence": 0.85,
            "sources": ["Analyst"],
            "chart": None,
            "insights": []
        }
        
        start_time = datetime.now()
        
        # =================================================================
        # 🧠 QDRANT RAG MEMORY CONTEXT
        # =================================================================
        historical_context = ""
        try:
            if vector_store.is_ready:
                past_chats = vector_store.search_chat_history(self.user_id, query, limit=3)
                if past_chats:
                    historical_context = "Historical AI Memory Context (from previous chats):\n"
                    for chat in past_chats:
                        role = chat.get('role', 'unknown').upper()
                        content = chat.get('content', '')[:300] # Truncate long messages
                        historical_context += f"[{role}]: {content}...\n"
                    
                    # Append it to the incoming context
                    context = f"{context}\n\n{historical_context}" if context else historical_context
                    logger.info("🧠 Injected Qdrant Semantic Memory into context")
        except Exception as e:
            logger.warning(f"Failed to inject Qdrant context: {e}")
        
        # =================================================================
        # �️ CHECK FOR IMAGE CONTEXT FIRST - Takes priority over data
        # =================================================================
        has_image_context = context and "πŸ–ΌοΈ Image Analysis" in context
        
        if has_image_context:
            logger.info("πŸ–ΌοΈ Analyst: Image context detected - routing to IMAGE ANALYSIS")
            
            if LLM_AVAILABLE:
                try:
                    image_prompt = f"""You are an AI assistant analyzing an image.

## IMAGE ANALYSIS CONTENT:
{context}

## USER QUESTION:
{query}

INSTRUCTIONS:
1. Answer based ONLY on the image analysis provided above
2. Describe what's visible in the image (objects, text, charts, patterns)
3. If the user asks "what do you see", describe the image content in detail
4. Extract any data, numbers, or text visible in the image
5. Be specific and accurate - don't make up things not in the image analysis

Provide a helpful, detailed response about the image."""

                    llm_response = llm_chat(image_prompt, temperature=0.3, max_tokens=800)
                    
                    result["answer"] = f"""## πŸ“Š Analyst - Image Analysis

{llm_response}

---
*πŸ–ΌοΈ This analysis is based on the uploaded image.*"""
                    result["sources"] = ["Analyst Engine", "Vision Analysis"]
                    result["confidence"] = 0.90
                    
                except Exception as e:
                    logger.error(f"Image analysis error: {e}")
                    result["answer"] = f"πŸ–ΌοΈ Image content:\n\n{context}"
            else:
                result["answer"] = f"πŸ–ΌοΈ Image Analysis:\n\n{context}"
            
            exec_time = (datetime.now() - start_time).total_seconds()
            result["execution_time"] = f"{exec_time:.2f}s"
            return result
        
        # =================================================================
        # οΏ½πŸ”„ DYNAMIC ROUTING: Check if query relates to actual data
        # =================================================================
        q_lower = query.lower()
        
        # Get column names from data
        column_names = [col.lower() for col in df.columns] if df is not None and not df.empty else []
        column_names_spaced = [col.replace('_', ' ') for col in column_names]
        
        # Check if query mentions ANY column or data-related term
        data_terms = column_names + column_names_spaced + [
            'my data', 'my ', 'our ', 'the data', 'uploaded', 'dataset',
            'total', 'sum', 'average', 'count', 'column', 'row'
        ]
        
        query_is_about_data = any(term in q_lower for term in data_terms if term)
        
        # =================================================================
        # 🌐 AI KNOWLEDGE PATH - Query is NOT about user's data
        # =================================================================
        
        if not query_is_about_data and df is not None:
            logger.info("🌐 Analyst: Routing to AI KNOWLEDGE (query not about user data)")
            
            if LLM_AVAILABLE:
                try:
                    # Check if visualization requested for AI knowledge
                    wants_viz = any(term in q_lower for term in [
                        'chart', 'graph', 'diagram', 'visualize', 'show me', 'draw',
                        'compare', 'breakdown', 'distribution', 'pie', 'bar'
                    ])
                    
                    if wants_viz:
                        ai_prompt = f"""You are a helpful AI assistant.

Answer this question and provide data that could be visualized:

{query}

Format your response with:
1. A clear answer
2. If applicable, provide key points with numbers that could be charted

Example format for chartable data:
- Category A: 40%
- Category B: 30%
- Category C: 20%
- Category D: 10%"""
                    else:
                        ai_prompt = f"""You are a helpful AI assistant with broad knowledge.

Answer this question clearly and helpfully:

{query}

Provide a clear, accurate, and informative response. Use bullet points if helpful."""

                    llm_response = llm_chat(ai_prompt, temperature=0.7, max_tokens=600)
                    
                    result["answer"] = f"""## 🌐 AI Knowledge

{llm_response}

---
*πŸ’‘ This is general AI knowledge. For analysis of YOUR data, ask about specific columns like {', '.join(column_names[:3]) if column_names else 'your metrics'}.*"""
                    
                    result["sources"] = ["AI Knowledge"]
                    result["confidence"] = 0.85
                    
                except Exception as e:
                    logger.error(f"AI Knowledge error: {e}")
                    result["answer"] = "🌐 I can help with that! Please ask your question again."
            else:
                result["answer"] = "🌐 AI Knowledge is not available. Please configure LLM."
            
            exec_time = (datetime.now() - start_time).total_seconds()
            result["execution_time"] = f"{exec_time:.2f}s"
            return result
        
        # =================================================================
        # πŸ“Š DATA PATH - Query IS about user's data
        # =================================================================
        
        logger.info("πŸ“Š Analyst: Routing to DATA ANALYSIS")
        
        # Check if we have data
        if df is None or df.empty:
            result["answer"] = self._no_data_response(query)
            return result
        
        # Analyze the query
        analysis = detect_analyst_intent(query, df)
        logger.info(f"🎯 Analyst Intent: {analysis.intent.value} (conf: {analysis.confidence:.0%})")
        
        # Calculate statistics
        stats = calculate_auto_statistics(df, analysis)
        
        # Generate chart ONLY if user explicitly requested
        chart = None
        smart_viz_result = None
        if generate_chart and analysis.wants_chart:
            # Try LLM visualizer first (dynamic chart generation like Claude)
            try:
                from core.llm_visualizer import llm_visualize
                viz_result = llm_visualize(df, query, self.user_id)
                if viz_result.get("success") and viz_result.get("chart"):
                    chart = viz_result.get("chart")
                    smart_viz_result = viz_result
                    logger.info(f"πŸ“Š LLM Chart: {viz_result.get('chart_type')}")
            except Exception as e:
                logger.warning(f"LLM visualizer error: {e}")
            
            # Fallback to SmartVisualization
            if not chart and SMART_VIZ_AVAILABLE:
                try:
                    smart_viz_result = smart_visualize(self.user_id, query, df)
                    if smart_viz_result.get("success"):
                        chart = smart_viz_result.get("chart")
                        logger.info(f"πŸ“Š Smart Chart: {smart_viz_result.get('visualization_type')}")
                except Exception as e:
                    logger.warning(f"SmartViz error: {e}")
            
            # Final fallback to rule-based chart
            if not chart:
                chart = generate_analyst_chart(df, analysis, stats)
            
            logger.info(f"πŸ“Š Chart generated: {analysis.chart_suggestion}")
        
        # Generate intelligent response
        response = self._generate_response(query, df, analysis, stats, context)
        
        # Build final result
        result["answer"] = response
        result["confidence"] = analysis.confidence
        result["chart"] = chart
        result["query_analysis"] = {
            "intent": analysis.intent.value,
            "target_columns": analysis.target_columns,
            "group_by": analysis.group_by,
            "aggregation": analysis.aggregation
        }
        
        # Add visualization recommendations if available
        if smart_viz_result:
            result["viz_type"] = smart_viz_result.get("visualization_type")
            result["viz_recommendations"] = smart_viz_result.get("recommendations", [])
        
        # EMBED CHART IN RESPONSE TEXT - Critical for frontend rendering
        if chart and analysis.wants_chart:
            # Embed chart JSON in response so frontend can render it
            if isinstance(chart, dict) and 'data' in chart and 'layout' in chart:
                import json
                chart_json = json.dumps(chart, default=str)
                result["answer"] += f"\n\n```plotly_chart\n{chart_json}\n```"
            else:
                viz_type = smart_viz_result.get("visualization_type", "chart") if smart_viz_result else "chart"
                result["answer"] += f"\n\n*πŸ“Š {viz_type.replace('_', ' ').title()} visualization generated.*"
            result["visualization"] = chart
        
        # Execution time
        exec_time = (datetime.now() - start_time).total_seconds()
        result["execution_time"] = f"{exec_time:.2f}s"
        
        # =================================================================
        # 🧠 SAVE TO QDRANT SEMANTIC MEMORY
        # =================================================================
        try:
            if vector_store.is_ready and query.strip():
                conv_id = f"rag_{int(start_time.timestamp())}"
                # Save user query
                vector_store.add_chat_message(self.user_id, "user", query, conv_id)
                
                # Save assistant response (truncate if too long to avoid massive embeddings)
                if result.get("answer"):
                    clean_answer = result["answer"][:1500]
                    vector_store.add_chat_message(self.user_id, "assistant", clean_answer, conv_id)
                logger.info("🧠 Saved interaction to Qdrant Semantic Memory")
        except Exception as e:
            logger.warning(f"Failed to save to Qdrant: {e}")
        
        return result
    
    def _generate_response(
        self,
        query: str,
        df: pd.DataFrame,
        analysis: QueryAnalysis,
        stats: Dict,
        context: str
    ) -> str:
        """
        Generate data-focused response.
        Note: AI Knowledge routing is now handled in process() method.
        """
        
        # Build data summary for LLM
        data_summary = f"""
Dataset: {stats['dataset_info']['rows']} rows, {stats['dataset_info']['columns']} columns

"""
        
        # Add computed metrics
        if 'computed_metrics' in stats and stats['computed_metrics']:
            data_summary += "**Key Metrics:**\n"
            for key, value in stats['computed_metrics'].items():
                if isinstance(value, float):
                    data_summary += f"- {key.replace('_', ' ').title()}: {value:,.2f}\n"
                else:
                    data_summary += f"- {key.replace('_', ' ').title()}: {value}\n"
        
        # Add numeric summary
        if 'numeric_summary' in stats:
            data_summary += "\n**Column Statistics:**\n"
            for col, col_stats in list(stats['numeric_summary'].items())[:5]:
                data_summary += f"- **{col}**: mean={col_stats['mean']:,.2f}, "
                data_summary += f"min={col_stats['min']:,.2f}, max={col_stats['max']:,.2f}\n"
        
        # Add breakdown if available
        if 'breakdown' in stats:
            data_summary += "\n**Breakdown:**\n"
            for key, value in list(stats['breakdown'].items())[:5]:
                data_summary += f"- {key}: {value:,}\n"
        
        # Use LLM to generate data-based response
        if LLM_AVAILABLE:
            if analysis.wants_brief:
                prompt = f"""Answer based on ONLY this data. Be brief.

Question: {query}

Data: {data_summary}

Give a SHORT, direct answer."""
            else:
                prompt = f"""You are a data analyst. Answer based on the user's actual data.

Question: {query}

πŸ“Š USER'S DATA:
{data_summary}

{context if context else ''}

Provide insights from the data. Be specific with actual values."""

            try:
                max_tokens = 50 if analysis.wants_brief else 500
                llm_response = llm_chat(prompt, temperature=0.3, max_tokens=max_tokens)
                
                if analysis.wants_brief:
                    return llm_response.strip()
                
                return f"## πŸ“Š Analysis\n\nπŸ“Š **From Your Data:**\n\n{llm_response}"
                
            except Exception as e:
                logger.error(f"LLM error: {e}")
        
        # Fallback response
        return f"## πŸ“Š Analysis\n\nπŸ“Š **From Your Data:**\n\n{data_summary}"
    
    def _no_data_response(self, query: str) -> str:
        """Response when no data is available."""
        return """## πŸ“Š Analyst

I need data to analyze! Please:

1. **Upload a file** in the Data Hub
2. **Use a file** from your uploads

Once you have data loaded, I can:
- πŸ“ˆ Summarize your data
- πŸ”’ Calculate statistics
- πŸ“Š Generate visualizations
- πŸ” Find patterns and insights

*Upload some data and ask me anything!*
"""


# =============================================================================
# CONVENIENCE FUNCTIONS
# =============================================================================

def analyst_response(
    user_id: str,
    query: str,
    context: str = "",
    df: pd.DataFrame = None
) -> Dict[str, Any]:
    """Cyclic Multi-Agent loop function for analyst response."""
    engine = ProAnalystEngine(user_id)
    
    # 🧠 Cyclic Reasoning Loop
    max_tries = 3
    current_context = context
    
    for attempt in range(max_tries):
        # 1. Analyst Generation
        result = engine.process(query, current_context, df)
        draft_answer = result.get('answer', '') if isinstance(result, dict) else str(result)
        
        # 2. Critic Evaluation
        try:
            from agents.critic import evaluate_with_critic
            critic_review = evaluate_with_critic(query, current_context, draft_answer)
            
            if critic_review.get('pass', True):
                print(f"[PASS] Critic Agent approved answer on attempt {attempt+1}")
                return result
            else:
                print(f"[FAIL] Critic Agent rejected answer on attempt {attempt+1}. Feedback: {critic_review.get('feedback')}")
                # Feed critic feedback back into the context for the next iteration
                critic_feedback = f"\n\n[CRITIC FEEDBACK: Your previous attempt failed. Fix this: {critic_review.get('feedback')}]"
                current_context += critic_feedback
                
                # If we're on the last attempt, just return it anyway but flag it
                if attempt == max_tries - 1:
                    print("[WARNING] Max attempts reached. Returning imperfect answer.")
                    if isinstance(result, dict):
                        result['answer'] += f"\n\n*(Note: This answer was flagged by the internal AI Critic for potential inaccuracies: {critic_review.get('feedback')})*"
                    return result
                    
        except Exception as e:
            print(f"⚠️ Critic loop error: {e}")
            return result
            
    return result


def analyst_response_sync(
    user_id: str,
    query: str,
    context: str = "",
    df: pd.DataFrame = None
) -> Dict[str, Any]:
    """Synchronous analyst response for compatibility."""
    return analyst_response(user_id, query, context, df)


# Alias for backwards compatibility
AnalystEngine = ProAnalystEngine

__all__ = ['ProAnalystEngine', 'AnalystEngine', 'analyst_response', 'analyst_response_sync']