File size: 77,504 Bytes
abb0fcc
 
b560569
575b933
b0464a9
87a87e7
791c130
 
266ae82
8673558
63031db
 
f7fc39b
575b933
266ae82
575b933
 
 
2601f1c
575b933
 
 
 
 
266ae82
2601f1c
 
9d99925
dc88746
3b4dccb
d33040c
3b4dccb
 
 
 
 
 
 
8673558
deb2291
266ae82
deb2291
 
c6716b6
 
 
 
3b4dccb
2601f1c
 
 
abb0fcc
2601f1c
 
b0464a9
2a3b22e
3b4dccb
2a3b22e
eb46c40
 
d33040c
 
 
eb46c40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2601f1c
 
eb46c40
 
092a033
 
 
 
 
63031db
 
 
092a033
 
63031db
 
092a033
 
abb0fcc
63031db
 
 
 
 
 
 
 
abb0fcc
63031db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
092a033
 
 
63031db
 
092a033
 
63031db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
092a033
63031db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
092a033
63031db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
abb0fcc
63031db
 
 
 
 
 
 
092a033
 
 
63031db
 
092a033
63031db
 
092a033
 
6a8e128
092a033
266ae82
dc88746
 
092a033
 
791c130
d33040c
791c130
d33040c
092a033
 
 
 
575b933
2601f1c
 
dc88746
092a033
3b4dccb
348bc84
791c130
 
092a033
 
 
 
 
 
 
 
 
 
 
791c130
d33040c
791c130
d33040c
092a033
 
 
2601f1c
791c130
 
266ae82
19ea45c
2601f1c
dc88746
 
092a033
dc88746
791c130
dc88746
 
abb0fcc
dc88746
 
 
 
 
 
 
 
 
 
abb0fcc
dc88746
 
 
 
 
 
092a033
dc88746
 
 
 
 
 
092a033
dc88746
092a033
dc88746
 
 
 
 
 
d33040c
365263e
d33040c
 
 
2601f1c
266ae82
dc88746
 
 
c6716b6
dc88746
 
 
eb46c40
092a033
dc88746
 
 
 
 
 
 
 
 
 
092a033
 
 
 
791c130
dc88746
 
791c130
dc88746
092a033
 
abb0fcc
092a033
a342a6b
b0464a9
2a3b22e
adb3bbe
abb0fcc
67742c4
a342a6b
6a8e128
 
 
 
 
2601f1c
67742c4
2601f1c
 
 
092a033
adb3bbe
a342a6b
d33040c
 
 
2601f1c
a342a6b
575b933
0612e1d
4ad44b9
266ae82
0612e1d
adb3bbe
791c130
 
d33040c
 
 
 
2601f1c
2a3b22e
4ad44b9
2a3b22e
a342a6b
 
2a3b22e
8673558
d33040c
 
2601f1c
d33040c
2601f1c
8673558
791c130
d33040c
 
791c130
365263e
092a033
 
8673558
d33040c
791c130
 
d33040c
3b902c0
 
791c130
 
 
 
 
2601f1c
266ae82
d33040c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
266ae82
d33040c
6a8e128
365263e
 
2601f1c
365263e
ddd95f0
8673558
 
6a8e128
2601f1c
365263e
 
2601f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
998bc4b
2601f1c
365263e
 
 
092a033
 
2601f1c
 
 
 
 
 
092a033
abb0fcc
 
998bc4b
 
c205383
2601f1c
 
266ae82
8673558
 
ddd95f0
8673558
2601f1c
 
 
 
 
 
 
abb0fcc
2601f1c
 
 
365263e
 
2601f1c
 
ddd95f0
 
eb46c40
2601f1c
365263e
d33040c
eb46c40
8673558
ddd95f0
2601f1c
 
 
 
 
 
 
092a033
 
365263e
abb0fcc
092a033
 
abb0fcc
 
 
 
 
 
 
 
 
 
 
 
092a033
abb0fcc
 
 
 
2601f1c
 
abb0fcc
 
092a033
 
abb0fcc
092a033
 
2601f1c
 
 
 
 
 
ddd95f0
2601f1c
eb46c40
2601f1c
eb46c40
 
2601f1c
 
 
eb46c40
2601f1c
eb46c40
2601f1c
 
365263e
2601f1c
 
 
8673558
 
092a033
2601f1c
 
eb46c40
2601f1c
092a033
2601f1c
 
 
 
ddd95f0
 
2601f1c
 
 
 
 
365263e
abb0fcc
 
2601f1c
ddd95f0
998bc4b
ddd95f0
2601f1c
 
365263e
 
092a033
2601f1c
 
 
092a033
 
365263e
2601f1c
 
 
092a033
 
 
 
2601f1c
 
 
092a033
 
 
 
 
 
 
 
abb0fcc
092a033
 
2601f1c
 
 
 
 
 
365263e
2601f1c
092a033
2601f1c
 
365263e
 
092a033
2601f1c
 
 
092a033
365263e
2601f1c
092a033
 
 
 
 
 
 
 
 
2601f1c
 
8673558
d33040c
eb46c40
d33040c
2601f1c
092a033
365263e
2601f1c
 
8673558
 
2601f1c
ddd95f0
eb46c40
d33040c
ddd95f0
eb46c40
d33040c
092a033
8673558
ddd95f0
 
 
8673558
365263e
2601f1c
365263e
8673558
eb46c40
8673558
365263e
8673558
092a033
365263e
ddd95f0
2601f1c
092a033
2601f1c
365263e
092a033
365263e
 
092a033
2601f1c
 
 
ddd95f0
365263e
8673558
 
2601f1c
 
365263e
abb0fcc
8673558
092a033
eb46c40
 
8673558
 
eb46c40
 
365263e
abb0fcc
ddd95f0
092a033
2601f1c
 
 
092a033
 
2601f1c
092a033
eb46c40
8673558
dc88746
092a033
dc88746
092a033
dc88746
 
092a033
998bc4b
ddd95f0
 
eb46c40
ddd95f0
998bc4b
ddd95f0
dc88746
365263e
2601f1c
ddd95f0
 
 
dc88746
8673558
2601f1c
ddd95f0
 
 
8673558
 
 
ddd95f0
 
8673558
d33040c
2601f1c
 
092a033
 
2601f1c
 
092a033
2601f1c
 
 
 
092a033
2601f1c
 
092a033
365263e
2601f1c
 
 
 
092a033
2601f1c
 
 
 
 
092a033
2601f1c
 
 
8673558
2601f1c
 
092a033
 
266ae82
092a033
266ae82
8673558
092a033
 
2601f1c
365263e
2601f1c
d33040c
8673558
365263e
 
d33040c
ddd95f0
2601f1c
092a033
abb0fcc
 
092a033
abb0fcc
 
 
092a033
 
 
abb0fcc
092a033
2601f1c
 
d33040c
ddd95f0
 
092a033
365263e
 
abb0fcc
365263e
abb0fcc
6a8e128
092a033
2601f1c
dc88746
266ae82
 
365263e
2601f1c
8673558
 
 
eb46c40
abb0fcc
8673558
 
abb0fcc
 
 
 
 
092a033
 
abb0fcc
 
 
 
 
ddd95f0
2601f1c
 
8673558
 
 
 
 
365263e
eb46c40
abb0fcc
2601f1c
092a033
2601f1c
 
6a8e128
791c130
266ae82
2601f1c
eb46c40
a342a6b
adb3bbe
06d22e5
d33040c
 
 
 
4ad44b9
 
eb46c40
a342a6b
 
575b933
d33040c
 
 
365263e
d33040c
a342a6b
d33040c
 
2601f1c
a342a6b
 
266ae82
a342a6b
538b42b
2601f1c
266ae82
 
ddd95f0
266ae82
365263e
 
8673558
365263e
266ae82
 
365263e
ddd95f0
266ae82
 
092a033
2601f1c
365263e
2601f1c
266ae82
 
adb3bbe
575b933
d33040c
575b933
 
 
d33040c
2601f1c
a342a6b
d33040c
365263e
092a033
2601f1c
abb0fcc
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
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
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# app.py 
# (Showing relevant parts that need modification)
import gradio as gr
import pandas as pd
import os
import logging
import matplotlib
matplotlib.use('Agg') # Set backend for Matplotlib to avoid GUI conflicts with Gradio
import matplotlib.pyplot as plt
import time # For profiling if needed
from datetime import datetime, timedelta # Added timedelta
import numpy as np

# --- Module Imports ---
from gradio_utils import get_url_user_token
# Functions from newly created/refactored modules
from config import (
    LINKEDIN_CLIENT_ID_ENV_VAR, BUBBLE_APP_NAME_ENV_VAR,
    BUBBLE_API_KEY_PRIVATE_ENV_VAR, BUBBLE_API_ENDPOINT_ENV_VAR)
from state_manager import process_and_store_bubble_token
from sync_logic import sync_all_linkedin_data_orchestrator
from ui_generators import (
    display_main_dashboard,
    run_mentions_tab_display,
    run_follower_stats_tab_display,
    build_analytics_tab_plot_area, 
    BOMB_ICON, EXPLORE_ICON, FORMULA_ICON, ACTIVE_ICON 
)
from analytics_data_processing import prepare_filtered_analytics_data # This is key for data structure
from analytics_plot_generator import (
    generate_posts_activity_plot, 
    generate_mentions_activity_plot, generate_mention_sentiment_plot,
    generate_followers_count_over_time_plot,
    generate_followers_growth_rate_plot,
    generate_followers_by_demographics_plot,
    generate_engagement_rate_over_time_plot,
    generate_reach_over_time_plot,
    generate_impressions_over_time_plot,
    create_placeholder_plot,
    generate_likes_over_time_plot,
    generate_clicks_over_time_plot,
    generate_shares_over_time_plot,
    generate_comments_over_time_plot,
    generate_comments_sentiment_breakdown_plot,
    generate_post_frequency_plot,
    generate_content_format_breakdown_plot,
    generate_content_topic_breakdown_plot
)
from formulas import PLOT_FORMULAS 

# --- NEW CHATBOT MODULE IMPORTS ---
from chatbot_prompts import get_initial_insight_prompt_and_suggestions # MODIFIED IMPORT
from chatbot_handler import generate_llm_response
# --- END NEW CHATBOT MODULE IMPORTS ---

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(module)s - %(message)s')

# Mapping from plot_configs IDs to PLOT_FORMULAS keys
PLOT_ID_TO_FORMULA_KEY_MAP = {
    "posts_activity": "posts_activity", 
    "mentions_activity": "mentions_activity", 
    "mention_sentiment": "mention_sentiment", 
    "followers_count": "followers_count_over_time",
    "followers_growth_rate": "followers_growth_rate",
    "followers_by_location": "followers_by_demographics",
    "followers_by_role": "followers_by_demographics",
    "followers_by_industry": "followers_by_demographics",
    "followers_by_seniority": "followers_by_demographics",
    "engagement_rate": "engagement_rate_over_time",
    "reach_over_time": "reach_over_time",
    "impressions_over_time": "impressions_over_time",
    "likes_over_time": "likes_over_time",
    "clicks_over_time": "clicks_over_time",
    "shares_over_time": "shares_over_time",
    "comments_over_time": "comments_over_time",
    "comments_sentiment": "comments_sentiment_breakdown",
    "post_frequency_cs": "post_frequency",
    "content_format_breakdown_cs": "content_format_breakdown",
    "content_topic_breakdown_cs": "content_topic_breakdown",
    "mention_analysis_volume": "mentions_activity", 
    "mention_analysis_sentiment": "mention_sentiment"
}

# --- Helper function to generate textual data summaries for chatbot ---
def generate_chatbot_data_summaries(
    plot_configs_list,
    filtered_merged_posts_df,
    filtered_mentions_df,
    date_filtered_follower_stats_df, # Expected to contain 'follower_gains_monthly'
    raw_follower_stats_df,          # Expected to contain other demographics like 'follower_geo', 'follower_industry'
    token_state_value
    ):
    """
    Generates textual summaries for each plot ID to be used by the chatbot,
    based on the corrected understanding of DataFrame structures and follower count columns.
    """
    data_summaries = {}
    
    # --- Date and Config Columns from token_state ---
    # For Posts
    date_col_posts = token_state_value.get("config_date_col_posts", "published_at")
    media_type_col_name = token_state_value.get("config_media_type_col", "media_type")
    eb_labels_col_name = token_state_value.get("config_eb_labels_col", "li_eb_label")
    # For Mentions
    date_col_mentions = token_state_value.get("config_date_col_mentions", "date")
    mentions_sentiment_col = "sentiment_label" # As per user's mention df structure
    
    # For Follower Stats - Actual column names provided by user
    follower_count_organic_col = "follower_count_organic"
    follower_count_paid_col = "follower_count_paid"

    # For Follower Stats (Demographics from raw_follower_stats_df)
    follower_demographics_type_col = "follower_count_type" # Column indicating 'follower_geo', 'follower_industry'
    follower_demographics_category_col = "category_name"   # Column indicating 'USA', 'Technology'

    # For Follower Gains/Growth (from date_filtered_follower_stats_df)
    follower_gains_type_col = "follower_count_type" # Should be 'follower_gains_monthly'
    follower_gains_date_col = "category_name"       # This is 'YYYY-MM-DD'

    # --- Helper: Safely convert to datetime ---
    def safe_to_datetime(series, errors='coerce'):
        return pd.to_datetime(series, errors=errors)

    # --- Prepare DataFrames (copy and convert dates) ---
    if filtered_merged_posts_df is not None and not filtered_merged_posts_df.empty:
        posts_df = filtered_merged_posts_df.copy()
        if date_col_posts in posts_df.columns:
            posts_df[date_col_posts] = safe_to_datetime(posts_df[date_col_posts])
        else:
            logging.warning(f"Date column '{date_col_posts}' not found in posts_df for chatbot summary.")
    else:
        posts_df = pd.DataFrame()

    if filtered_mentions_df is not None and not filtered_mentions_df.empty:
        mentions_df = filtered_mentions_df.copy()
        if date_col_mentions in mentions_df.columns:
            mentions_df[date_col_mentions] = safe_to_datetime(mentions_df[date_col_mentions])
        else:
            logging.warning(f"Date column '{date_col_mentions}' not found in mentions_df for chatbot summary.")
    else:
        mentions_df = pd.DataFrame()

    # For date_filtered_follower_stats_df (monthly gains)
    if date_filtered_follower_stats_df is not None and not date_filtered_follower_stats_df.empty:
        follower_monthly_df = date_filtered_follower_stats_df.copy()
        if follower_gains_type_col in follower_monthly_df.columns:
             follower_monthly_df = follower_monthly_df[follower_monthly_df[follower_gains_type_col] == 'follower_gains_monthly'].copy()
        
        if follower_gains_date_col in follower_monthly_df.columns:
            follower_monthly_df['datetime_obj'] = safe_to_datetime(follower_monthly_df[follower_gains_date_col])
            follower_monthly_df = follower_monthly_df.dropna(subset=['datetime_obj'])
            
            # Calculate total gains
            if follower_count_organic_col in follower_monthly_df.columns and follower_count_paid_col in follower_monthly_df.columns:
                follower_monthly_df[follower_count_organic_col] = pd.to_numeric(follower_monthly_df[follower_count_organic_col], errors='coerce').fillna(0)
                follower_monthly_df[follower_count_paid_col] = pd.to_numeric(follower_monthly_df[follower_count_paid_col], errors='coerce').fillna(0)
                follower_monthly_df['total_monthly_gains'] = follower_monthly_df[follower_count_organic_col] + follower_monthly_df[follower_count_paid_col]
            elif follower_count_organic_col in follower_monthly_df.columns: # Only organic exists
                 follower_monthly_df[follower_count_organic_col] = pd.to_numeric(follower_monthly_df[follower_count_organic_col], errors='coerce').fillna(0)
                 follower_monthly_df['total_monthly_gains'] = follower_monthly_df[follower_count_organic_col]
            elif follower_count_paid_col in follower_monthly_df.columns: # Only paid exists
                 follower_monthly_df[follower_count_paid_col] = pd.to_numeric(follower_monthly_df[follower_count_paid_col], errors='coerce').fillna(0)
                 follower_monthly_df['total_monthly_gains'] = follower_monthly_df[follower_count_paid_col]
            else:
                logging.warning(f"Neither '{follower_count_organic_col}' nor '{follower_count_paid_col}' found in follower_monthly_df for total gains calculation.")
                follower_monthly_df['total_monthly_gains'] = 0 # Avoid KeyError later
        else:
            logging.warning(f"Date column '{follower_gains_date_col}' (from category_name) not found in follower_monthly_df for chatbot summary.")
            if 'datetime_obj' not in follower_monthly_df.columns:
                 follower_monthly_df['datetime_obj'] = pd.NaT 
            if 'total_monthly_gains' not in follower_monthly_df.columns:
                 follower_monthly_df['total_monthly_gains'] = 0
    else:
        follower_monthly_df = pd.DataFrame(columns=[follower_gains_date_col, 'total_monthly_gains', 'datetime_obj'])


    if raw_follower_stats_df is not None and not raw_follower_stats_df.empty:
        follower_demographics_df = raw_follower_stats_df.copy()
        # Calculate total followers for demographics
        if follower_count_organic_col in follower_demographics_df.columns and follower_count_paid_col in follower_demographics_df.columns:
            follower_demographics_df[follower_count_organic_col] = pd.to_numeric(follower_demographics_df[follower_count_organic_col], errors='coerce').fillna(0)
            follower_demographics_df[follower_count_paid_col] = pd.to_numeric(follower_demographics_df[follower_count_paid_col], errors='coerce').fillna(0)
            follower_demographics_df['total_follower_count'] = follower_demographics_df[follower_count_organic_col] + follower_demographics_df[follower_count_paid_col]
        elif follower_count_organic_col in follower_demographics_df.columns:
            follower_demographics_df[follower_count_organic_col] = pd.to_numeric(follower_demographics_df[follower_count_organic_col], errors='coerce').fillna(0)
            follower_demographics_df['total_follower_count'] = follower_demographics_df[follower_count_organic_col]
        elif follower_count_paid_col in follower_demographics_df.columns:
            follower_demographics_df[follower_count_paid_col] = pd.to_numeric(follower_demographics_df[follower_count_paid_col], errors='coerce').fillna(0)
            follower_demographics_df['total_follower_count'] = follower_demographics_df[follower_count_paid_col]
        else:
            logging.warning(f"Neither '{follower_count_organic_col}' nor '{follower_count_paid_col}' found in follower_demographics_df for total count calculation.")
            if 'total_follower_count' not in follower_demographics_df.columns:
                 follower_demographics_df['total_follower_count'] = 0
    else:
        follower_demographics_df = pd.DataFrame()


    for plot_cfg in plot_configs_list:
        plot_id = plot_cfg["id"]
        plot_label = plot_cfg["label"]
        summary_text = f"No specific data summary available for '{plot_label}' for the selected period."

        try:
            # --- FOLLOWER STATS ---
            if plot_id == "followers_count": # Uses follower_monthly_df
                if not follower_monthly_df.empty and 'total_monthly_gains' in follower_monthly_df.columns and 'datetime_obj' in follower_monthly_df.columns and not follower_monthly_df['datetime_obj'].isnull().all():
                    df_summary = follower_monthly_df[['datetime_obj', 'total_monthly_gains']].copy()
                    df_summary['datetime_obj'] = df_summary['datetime_obj'].dt.strftime('%Y-%m-%d')
                    df_summary.rename(columns={'datetime_obj': 'Date', 'total_monthly_gains': 'Total Monthly Gains'}, inplace=True)
                    summary_text = f"Follower Count (Total Monthly Gains):\n{df_summary.sort_values(by='Date').tail(5).to_string(index=False)}"
                else:
                    summary_text = f"Follower count data (total monthly gains) is unavailable or incomplete for '{plot_label}'."
            
            elif plot_id == "followers_growth_rate": # Uses follower_monthly_df
                if not follower_monthly_df.empty and 'total_monthly_gains' in follower_monthly_df.columns and 'datetime_obj' in follower_monthly_df.columns and not follower_monthly_df['datetime_obj'].isnull().all():
                    df_calc = follower_monthly_df.sort_values(by='datetime_obj').copy()
                    # Growth rate is calculated on the total monthly gains (which are changes, not cumulative counts)
                    # To calculate growth rate of followers, we'd need cumulative follower count.
                    # The plot logic also uses pct_change on the gains themselves.
                    # If 'total_monthly_gains' represents the *change* in followers, then pct_change on this is rate of change of gains.
                    # If it represents the *cumulative* followers at that point, then pct_change is follower growth rate.
                    # Assuming 'total_monthly_gains' is the *change* for the month, like the plot logic.
                    df_calc['total_monthly_gains'] = pd.to_numeric(df_calc['total_monthly_gains'], errors='coerce')
                    if len(df_calc) >= 2:
                        # Calculate cumulative sum to get follower count if 'total_monthly_gains' are indeed just gains
                        # If your 'total_monthly_gains' already IS the total follower count at end of month, remove next line
                        # For now, assuming it's GAINS, so we need cumulative for growth rate of total followers.
                        # However, the original plot logic applies pct_change directly to 'follower_gains_monthly'.
                        # Let's stick to pct_change on the gains/count column for consistency with plot.
                        
                        # If 'total_monthly_gains' is the actual follower count for that month:
                        df_calc['growth_rate_monthly'] = df_calc['total_monthly_gains'].pct_change() * 100
                        df_calc['growth_rate_monthly'] = df_calc['growth_rate_monthly'].round(2)
                        df_calc.replace([np.inf, -np.inf], np.nan, inplace=True) # Handle division by zero if a gain was 0
                        
                        df_summary = df_calc[['datetime_obj', 'growth_rate_monthly']].dropna().copy()
                        df_summary['datetime_obj'] = df_summary['datetime_obj'].dt.strftime('%Y-%m-%d')
                        df_summary.rename(columns={'datetime_obj': 'Date', 'growth_rate_monthly': 'Growth Rate (%)'}, inplace=True)
                        if not df_summary.empty:
                            summary_text = f"Follower Growth Rate (Monthly % based on Total Follower Count/Gains):\n{df_summary.sort_values(by='Date').tail(5).to_string(index=False)}"
                        else:
                            summary_text = f"Not enough data points or valid transitions to calculate follower growth rate for '{plot_label}'."
                    else:
                        summary_text = f"Not enough data points (need at least 2) to calculate follower growth rate for '{plot_label}'."
                else:
                    summary_text = f"Follower growth rate data (total monthly gains) is unavailable or incomplete for '{plot_label}'."

            elif plot_id in ["followers_by_location", "followers_by_role", "followers_by_industry", "followers_by_seniority"]:
                demographic_type_map = {
                    "followers_by_location": "follower_geo",
                    "followers_by_role": "follower_function",
                    "followers_by_industry": "follower_industry",
                    "followers_by_seniority": "follower_seniority"
                }
                current_demographic_type = demographic_type_map.get(plot_id)
                if not follower_demographics_df.empty and \
                   follower_demographics_type_col in follower_demographics_df.columns and \
                   follower_demographics_category_col in follower_demographics_df.columns and \
                   'total_follower_count' in follower_demographics_df.columns: # Check for the calculated total
                    
                    df_filtered_demographics = follower_demographics_df[
                        follower_demographics_df[follower_demographics_type_col] == current_demographic_type
                    ].copy()

                    if not df_filtered_demographics.empty:
                        df_summary = df_filtered_demographics.groupby(follower_demographics_category_col)['total_follower_count'].sum().reset_index()
                        df_summary.rename(columns={follower_demographics_category_col: 'Category', 'total_follower_count': 'Total Follower Count'}, inplace=True)
                        top_5 = df_summary.nlargest(5, 'Total Follower Count')
                        summary_text = f"Top 5 {plot_label} (Total Followers):\n{top_5.to_string(index=False)}"
                    else:
                        summary_text = f"No data available for demographic type '{current_demographic_type}' in '{plot_label}'."
                else:
                    summary_text = f"Follower demographic data columns (including total_follower_count) are missing or incomplete for '{plot_label}'."

            # --- POSTS STATS ---
            elif plot_id == "engagement_rate":
                if not posts_df.empty and 'engagement' in posts_df.columns and date_col_posts in posts_df.columns and not posts_df[date_col_posts].isnull().all():
                    df_resampled = posts_df.set_index(date_col_posts)['engagement'].resample('W').mean().reset_index()
                    df_resampled['engagement'] = pd.to_numeric(df_resampled['engagement'], errors='coerce').round(2)
                    df_summary = df_resampled[[date_col_posts, 'engagement']].dropna().copy()
                    df_summary[date_col_posts] = df_summary[date_col_posts].dt.strftime('%Y-%m-%d')
                    summary_text = f"Engagement Rate Over Time (Weekly Avg %):\n{df_summary.sort_values(by=date_col_posts).tail(5).to_string(index=False)}"
                else:
                    summary_text = f"Engagement rate data is unavailable for '{plot_label}'."
            
            elif plot_id == "reach_over_time": 
                if not posts_df.empty and 'reach' in posts_df.columns and date_col_posts in posts_df.columns and not posts_df[date_col_posts].isnull().all():
                    df_resampled = posts_df.set_index(date_col_posts)['reach'].resample('W').sum().reset_index()
                    df_resampled['reach'] = pd.to_numeric(df_resampled['reach'], errors='coerce')
                    df_summary = df_resampled[[date_col_posts, 'reach']].dropna().copy()
                    df_summary[date_col_posts] = df_summary[date_col_posts].dt.strftime('%Y-%m-%d')
                    summary_text = f"Reach Over Time (Weekly Sum):\n{df_summary.sort_values(by=date_col_posts).tail(5).to_string(index=False)}"
                else:
                    summary_text = f"Reach data is unavailable for '{plot_label}'."

            elif plot_id == "impressions_over_time": 
                if not posts_df.empty and 'impressionCount' in posts_df.columns and date_col_posts in posts_df.columns and not posts_df[date_col_posts].isnull().all():
                    df_resampled = posts_df.set_index(date_col_posts)['impressionCount'].resample('W').sum().reset_index()
                    df_resampled['impressionCount'] = pd.to_numeric(df_resampled['impressionCount'], errors='coerce')
                    df_summary = df_resampled[[date_col_posts, 'impressionCount']].dropna().copy()
                    df_summary[date_col_posts] = df_summary[date_col_posts].dt.strftime('%Y-%m-%d')
                    df_summary.rename(columns={'impressionCount': 'Impressions'}, inplace=True)
                    summary_text = f"Impressions Over Time (Weekly Sum):\n{df_summary.sort_values(by=date_col_posts).tail(5).to_string(index=False)}"
                else:
                    summary_text = f"Impressions data is unavailable for '{plot_label}'."

            elif plot_id == "likes_over_time": 
                if not posts_df.empty and 'likeCount' in posts_df.columns and date_col_posts in posts_df.columns and not posts_df[date_col_posts].isnull().all():
                    df_resampled = posts_df.set_index(date_col_posts)['likeCount'].resample('W').sum().reset_index()
                    df_resampled['likeCount'] = pd.to_numeric(df_resampled['likeCount'], errors='coerce')
                    df_summary = df_resampled[[date_col_posts, 'likeCount']].dropna().copy()
                    df_summary[date_col_posts] = df_summary[date_col_posts].dt.strftime('%Y-%m-%d')
                    df_summary.rename(columns={'likeCount': 'Likes'}, inplace=True)
                    summary_text = f"Likes Over Time (Weekly Sum):\n{df_summary.sort_values(by=date_col_posts).tail(5).to_string(index=False)}"
                else:
                    summary_text = f"Likes data is unavailable for '{plot_label}'."

            elif plot_id == "clicks_over_time": 
                if not posts_df.empty and 'clickCount' in posts_df.columns and date_col_posts in posts_df.columns and not posts_df[date_col_posts].isnull().all():
                    df_resampled = posts_df.set_index(date_col_posts)['clickCount'].resample('W').sum().reset_index()
                    df_resampled['clickCount'] = pd.to_numeric(df_resampled['clickCount'], errors='coerce')
                    df_summary = df_resampled[[date_col_posts, 'clickCount']].dropna().copy()
                    df_summary[date_col_posts] = df_summary[date_col_posts].dt.strftime('%Y-%m-%d')
                    df_summary.rename(columns={'clickCount': 'Clicks'}, inplace=True)
                    summary_text = f"Clicks Over Time (Weekly Sum):\n{df_summary.sort_values(by=date_col_posts).tail(5).to_string(index=False)}"
                else:
                    summary_text = f"Clicks data is unavailable for '{plot_label}'."

            elif plot_id == "shares_over_time": 
                if not posts_df.empty and 'shareCount' in posts_df.columns and date_col_posts in posts_df.columns and not posts_df[date_col_posts].isnull().all():
                    df_resampled = posts_df.set_index(date_col_posts)['shareCount'].resample('W').sum().reset_index()
                    df_resampled['shareCount'] = pd.to_numeric(df_resampled['shareCount'], errors='coerce')
                    df_summary = df_resampled[[date_col_posts, 'shareCount']].dropna().copy()
                    df_summary[date_col_posts] = df_summary[date_col_posts].dt.strftime('%Y-%m-%d')
                    df_summary.rename(columns={'shareCount': 'Shares'}, inplace=True)
                    summary_text = f"Shares Over Time (Weekly Sum):\n{df_summary.sort_values(by=date_col_posts).tail(5).to_string(index=False)}"
                elif 'shareCount' not in posts_df.columns and not posts_df.empty : # Check if posts_df is not empty before assuming column is the only issue
                     summary_text = f"Shares data column ('shareCount') not found for '{plot_label}'."
                else:
                    summary_text = f"Shares data is unavailable for '{plot_label}'."
            
            elif plot_id == "comments_over_time": 
                if not posts_df.empty and 'commentCount' in posts_df.columns and date_col_posts in posts_df.columns and not posts_df[date_col_posts].isnull().all():
                    df_resampled = posts_df.set_index(date_col_posts)['commentCount'].resample('W').sum().reset_index()
                    df_resampled['commentCount'] = pd.to_numeric(df_resampled['commentCount'], errors='coerce')
                    df_summary = df_resampled[[date_col_posts, 'commentCount']].dropna().copy()
                    df_summary[date_col_posts] = df_summary[date_col_posts].dt.strftime('%Y-%m-%d')
                    df_summary.rename(columns={'commentCount': 'Comments'}, inplace=True)
                    summary_text = f"Comments Over Time (Weekly Sum):\n{df_summary.sort_values(by=date_col_posts).tail(5).to_string(index=False)}"
                else:
                    summary_text = f"Comments data is unavailable for '{plot_label}'."

            elif plot_id == "comments_sentiment": 
                comment_sentiment_col_posts = "sentiment" 
                if not posts_df.empty and comment_sentiment_col_posts in posts_df.columns:
                    sentiment_counts = posts_df[comment_sentiment_col_posts].value_counts().reset_index()
                    sentiment_counts.columns = ['Sentiment', 'Count']
                    summary_text = f"Comments Sentiment Breakdown (Posts Data):\n{sentiment_counts.to_string(index=False)}"
                else:
                    summary_text = f"Comment sentiment data ('{comment_sentiment_col_posts}') is unavailable for '{plot_label}'."
            
            elif plot_id == "post_frequency_cs":
                if not posts_df.empty and date_col_posts in posts_df.columns and not posts_df[date_col_posts].isnull().all():
                    post_counts_weekly = posts_df.set_index(date_col_posts).resample('W').size().reset_index(name='post_count')
                    post_counts_weekly.rename(columns={date_col_posts: 'Week', 'post_count': 'Posts'}, inplace=True)
                    post_counts_weekly['Week'] = post_counts_weekly['Week'].dt.strftime('%Y-%m-%d (Week of)')
                    summary_text = f"Post Frequency (Weekly):\n{post_counts_weekly.sort_values(by='Week').tail(5).to_string(index=False)}"
                else:
                    summary_text = f"Post frequency data is unavailable for '{plot_label}'."

            elif plot_id == "content_format_breakdown_cs":
                if not posts_df.empty and media_type_col_name in posts_df.columns:
                    format_counts = posts_df[media_type_col_name].value_counts().reset_index()
                    format_counts.columns = ['Format', 'Count']
                    summary_text = f"Content Format Breakdown:\n{format_counts.nlargest(5, 'Count').to_string(index=False)}"
                else:
                    summary_text = f"Content format data ('{media_type_col_name}') is unavailable for '{plot_label}'."

            elif plot_id == "content_topic_breakdown_cs":
                if not posts_df.empty and eb_labels_col_name in posts_df.columns:
                    try:
                        # Ensure the column is not all NaN before trying to check for lists or explode
                        if posts_df[eb_labels_col_name].notna().any():
                            if posts_df[eb_labels_col_name].apply(lambda x: isinstance(x, list)).any():
                                topic_counts = posts_df.explode(eb_labels_col_name)[eb_labels_col_name].value_counts().reset_index()
                            else: 
                                topic_counts = posts_df[eb_labels_col_name].value_counts().reset_index()
                            topic_counts.columns = ['Topic', 'Count']
                            summary_text = f"Content Topic Breakdown (Top 5):\n{topic_counts.nlargest(5, 'Count').to_string(index=False)}"
                        else:
                            summary_text = f"Content topic data ('{eb_labels_col_name}') contains no valid topics for '{plot_label}'."
                    except Exception as e_topic:
                        logging.warning(f"Could not process topic breakdown for '{eb_labels_col_name}': {e_topic}")
                        summary_text = f"Content topic data ('{eb_labels_col_name}') could not be processed for '{plot_label}'."
                else:
                    summary_text = f"Content topic data ('{eb_labels_col_name}') is unavailable for '{plot_label}'."

            # --- MENTIONS STATS ---
            elif plot_id == "mention_analysis_volume": 
                if not mentions_df.empty and date_col_mentions in mentions_df.columns and not mentions_df[date_col_mentions].isnull().all():
                    mentions_over_time = mentions_df.set_index(date_col_mentions).resample('W').size().reset_index(name='mention_count')
                    mentions_over_time.rename(columns={date_col_mentions: 'Week', 'mention_count': 'Mentions'}, inplace=True)
                    mentions_over_time['Week'] = mentions_over_time['Week'].dt.strftime('%Y-%m-%d (Week of)')
                    if not mentions_over_time.empty:
                        summary_text = f"Mentions Volume (Weekly):\n{mentions_over_time.sort_values(by='Week').tail(5).to_string(index=False)}"
                    else:
                        summary_text = f"No mention activity found for '{plot_label}' in the selected period."
                else:
                    summary_text = f"Mentions volume data is unavailable for '{plot_label}'."
            
            elif plot_id == "mention_analysis_sentiment": 
                if not mentions_df.empty and mentions_sentiment_col in mentions_df.columns:
                    sentiment_counts = mentions_df[mentions_sentiment_col].value_counts().reset_index()
                    sentiment_counts.columns = ['Sentiment', 'Count']
                    summary_text = f"Mentions Sentiment Breakdown:\n{sentiment_counts.to_string(index=False)}"
                else:
                    summary_text = f"Mention sentiment data ('{mentions_sentiment_col}') is unavailable for '{plot_label}'."

            data_summaries[plot_id] = summary_text
        except KeyError as e:
            logging.warning(f"KeyError generating summary for {plot_id} ('{plot_label}'): {e}. Using default summary.")
            data_summaries[plot_id] = f"Data summary generation error for '{plot_label}' (missing column: {e})."
        except Exception as e:
            logging.error(f"Error generating summary for {plot_id} ('{plot_label}'): {e}", exc_info=True)
            data_summaries[plot_id] = f"Error generating data summary for '{plot_label}'."
            
    return data_summaries
# --- Analytics Tab: Plot Figure Generation Function ---
def update_analytics_plots_figures(token_state_value, date_filter_option, custom_start_date, custom_end_date, current_plot_configs):
    logging.info(f"Updating analytics plot figures. Filter: {date_filter_option}, Custom Start: {custom_start_date}, Custom End: {custom_end_date}")
    num_expected_plots = 19 # Ensure this matches the number of plots generated
    
    plot_data_summaries_for_chatbot = {} # Initialize dict for chatbot summaries

    if not token_state_value or not token_state_value.get("token"):
        message = "❌ Accesso negato. Nessun token. Impossibile generare le analisi."
        logging.warning(message)
        placeholder_figs = [create_placeholder_plot(title="Accesso Negato", message="Nessun token.") for _ in range(num_expected_plots)]
        # For each plot_config, add a default "no data" summary
        for p_cfg in current_plot_configs:
            plot_data_summaries_for_chatbot[p_cfg["id"]] = "Accesso negato, nessun dato per il chatbot."
        return [message] + placeholder_figs + [plot_data_summaries_for_chatbot]
    try:
        (filtered_merged_posts_df, 
         filtered_mentions_df, 
         date_filtered_follower_stats_df, # For time-based follower plots
         raw_follower_stats_df,       # For demographic follower plots
         start_dt_for_msg, end_dt_for_msg) = \
            prepare_filtered_analytics_data(
                token_state_value, date_filter_option, custom_start_date, custom_end_date
            )
        
        # Generate data summaries for chatbot AFTER data preparation
        plot_data_summaries_for_chatbot = generate_chatbot_data_summaries(
            current_plot_configs, # Pass the plot_configs list
            filtered_merged_posts_df,
            filtered_mentions_df,
            date_filtered_follower_stats_df,
            raw_follower_stats_df,
            token_state_value
        )

    except Exception as e:
        error_msg = f"❌ Errore durante la preparazione dei dati per le analisi: {e}"
        logging.error(error_msg, exc_info=True)
        placeholder_figs = [create_placeholder_plot(title="Errore Preparazione Dati", message=str(e)) for _ in range(num_expected_plots)]
        for p_cfg in current_plot_configs:
            plot_data_summaries_for_chatbot[p_cfg["id"]] = f"Errore preparazione dati: {e}"
        return [error_msg] + placeholder_figs + [plot_data_summaries_for_chatbot]
    
    date_column_posts = token_state_value.get("config_date_col_posts", "published_at")
    date_column_mentions = token_state_value.get("config_date_col_mentions", "date")
    media_type_col_name = token_state_value.get("config_media_type_col", "media_type")
    eb_labels_col_name = token_state_value.get("config_eb_labels_col", "li_eb_label")
    
    plot_figs = [] # Initialize list to hold plot figures
    
    plot_titles_for_errors = [p_cfg["label"] for p_cfg in current_plot_configs] 

    try:
        # Dinamiche dei Follower (2 plots)
        plot_figs.append(generate_followers_count_over_time_plot(date_filtered_follower_stats_df, type_value='follower_gains_monthly'))
        plot_figs.append(generate_followers_growth_rate_plot(date_filtered_follower_stats_df, type_value='follower_gains_monthly')) # Assuming this uses 'follower_gains_monthly' to calculate rate
        
        # Demografia Follower (4 plots)
        plot_figs.append(generate_followers_by_demographics_plot(raw_follower_stats_df, type_value='follower_geo', plot_title="Follower per Località"))
        plot_figs.append(generate_followers_by_demographics_plot(raw_follower_stats_df, type_value='follower_function', plot_title="Follower per Ruolo"))
        plot_figs.append(generate_followers_by_demographics_plot(raw_follower_stats_df, type_value='follower_industry', plot_title="Follower per Settore"))
        plot_figs.append(generate_followers_by_demographics_plot(raw_follower_stats_df, type_value='follower_seniority', plot_title="Follower per Anzianità"))

        # Approfondimenti Performance Post (4 plots)
        plot_figs.append(generate_engagement_rate_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts))
        plot_figs.append(generate_reach_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts))
        plot_figs.append(generate_impressions_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts)) # Ensure 'impressions_sum' or equivalent is used by this func
        plot_figs.append(generate_likes_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts))
        
        # Engagement Dettagliato Post nel Tempo (4 plots)
        plot_figs.append(generate_clicks_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts))
        plot_figs.append(generate_shares_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts))
        plot_figs.append(generate_comments_over_time_plot(filtered_merged_posts_df, date_column=date_column_posts))
        plot_figs.append(generate_comments_sentiment_breakdown_plot(filtered_merged_posts_df, sentiment_column='comment_sentiment')) # Make sure 'comment_sentiment' exists
        
        # Analisi Strategia Contenuti (3 plots)
        plot_figs.append(generate_post_frequency_plot(filtered_merged_posts_df, date_column=date_column_posts))
        plot_figs.append(generate_content_format_breakdown_plot(filtered_merged_posts_df, format_col=media_type_col_name))
        plot_figs.append(generate_content_topic_breakdown_plot(filtered_merged_posts_df, topics_col=eb_labels_col_name))
        
        # Analisi Menzioni (Dettaglio) (2 plots)
        plot_figs.append(generate_mentions_activity_plot(filtered_mentions_df, date_column=date_column_mentions))
        plot_figs.append(generate_mention_sentiment_plot(filtered_mentions_df)) # Make sure this function handles empty/malformed df

        if len(plot_figs) != num_expected_plots:
            logging.warning(f"Mismatch in generated plots. Expected {num_expected_plots}, got {len(plot_figs)}. This will cause UI update issues.")
            while len(plot_figs) < num_expected_plots:
                plot_figs.append(create_placeholder_plot(title="Grafico Non Generato", message="Logica di generazione incompleta."))
        
        message = f"📊 Analisi aggiornate per il periodo: {date_filter_option}"
        if date_filter_option == "Intervallo Personalizzato": 
            s_display = start_dt_for_msg.strftime('%Y-%m-%d') if start_dt_for_msg else "Qualsiasi"
            e_display = end_dt_for_msg.strftime('%Y-%m-%d') if end_dt_for_msg else "Qualsiasi"
            message += f" (Da: {s_display} A: {e_display})"
        
        final_plot_figs = []
        for i, p_fig_candidate in enumerate(plot_figs):
            if p_fig_candidate is not None and not isinstance(p_fig_candidate, str): # Basic check for a plot object
                final_plot_figs.append(p_fig_candidate)
            else:
                err_title = plot_titles_for_errors[i] if i < len(plot_titles_for_errors) else f"Grafico {i+1}"
                logging.warning(f"Plot {err_title} (index {i}) non è una figura valida: {p_fig_candidate}. Uso placeholder.")
                final_plot_figs.append(create_placeholder_plot(title=f"Errore: {err_title}", message="Impossibile generare figura."))
        
        return [message] + final_plot_figs[:num_expected_plots] + [plot_data_summaries_for_chatbot]

    except (KeyError, ValueError) as e_plot_data:
        logging.error(f"Errore dati durante la generazione di un grafico specifico: {e_plot_data}", exc_info=True)
        error_msg_display = f"Errore dati in un grafico: {str(e_plot_data)[:100]}"
        
        num_already_generated = len(plot_figs)
        for i in range(num_already_generated, num_expected_plots):
            err_title_fill = plot_titles_for_errors[i] if i < len(plot_titles_for_errors) else f"Grafico {i+1}"
            plot_figs.append(create_placeholder_plot(title=f"Errore Dati: {err_title_fill}", message=f"Precedente errore: {str(e_plot_data)[:50]}"))
        
        for p_cfg in current_plot_configs: # Ensure summaries dict is populated on error
            if p_cfg["id"] not in plot_data_summaries_for_chatbot:
                 plot_data_summaries_for_chatbot[p_cfg["id"]] = f"Errore dati grafico: {e_plot_data}"
        return [error_msg_display] + plot_figs[:num_expected_plots] + [plot_data_summaries_for_chatbot]

    except Exception as e_general:
        error_msg = f"❌ Errore generale durante la generazione dei grafici: {e_general}"
        logging.error(error_msg, exc_info=True)
        placeholder_figs_general = [create_placeholder_plot(title=plot_titles_for_errors[i] if i < len(plot_titles_for_errors) else f"Grafico {i+1}", message=str(e_general)) for i in range(num_expected_plots)]
        for p_cfg in current_plot_configs: # Ensure summaries dict is populated on error
             if p_cfg["id"] not in plot_data_summaries_for_chatbot:
                 plot_data_summaries_for_chatbot[p_cfg["id"]] = f"Errore generale grafici: {e_general}"
        return [error_msg] + placeholder_figs_general + [plot_data_summaries_for_chatbot]


# --- Gradio UI Blocks ---
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
                title="LinkedIn Organization Dashboard") as app:
    token_state = gr.State(value={
        "token": None, "client_id": None, "org_urn": None,
        "bubble_posts_df": pd.DataFrame(), "bubble_post_stats_df": pd.DataFrame(),
        "bubble_mentions_df": pd.DataFrame(), "bubble_follower_stats_df": pd.DataFrame(),
        "fetch_count_for_api": 0, "url_user_token_temp_storage": None,
        "config_date_col_posts": "published_at", "config_date_col_mentions": "date",
        "config_date_col_followers": "date", "config_media_type_col": "media_type",
        "config_eb_labels_col": "li_eb_label"
    })
    
    chat_histories_st = gr.State({}) 
    current_chat_plot_id_st = gr.State(None) 
    plot_data_for_chatbot_st = gr.State({}) # NEW: Store data summaries for chatbot

    gr.Markdown("# 🚀 LinkedIn Organization Dashboard")
    url_user_token_display = gr.Textbox(label="User Token (Nascosto)", interactive=False, visible=False)
    status_box = gr.Textbox(label="Stato Generale Token LinkedIn", interactive=False, value="Inizializzazione...")
    org_urn_display = gr.Textbox(label="URN Organizzazione (Nascosto)", interactive=False, visible=False)
    
    app.load(fn=get_url_user_token, inputs=None, outputs=[url_user_token_display, org_urn_display], api_name="get_url_params", show_progress=False)

    def initial_load_sequence(url_token, org_urn_val, current_state):
        status_msg, new_state, btn_update = process_and_store_bubble_token(url_token, org_urn_val, current_state)
        dashboard_content = display_main_dashboard(new_state)
        return status_msg, new_state, btn_update, dashboard_content

    with gr.Tabs() as tabs:
        with gr.TabItem("1️⃣ Dashboard & Sync", id="tab_dashboard_sync"):
            gr.Markdown("Il sistema controlla i dati esistenti da Bubble. 'Sincronizza' si attiva se sono necessari nuovi dati.")
            sync_data_btn = gr.Button("🔄 Sincronizza Dati LinkedIn", variant="primary", visible=False, interactive=False)
            sync_status_html_output = gr.HTML("<p style='text-align:center;'>Stato sincronizzazione...</p>")
            dashboard_display_html = gr.HTML("<p style='text-align:center;'>Caricamento dashboard...</p>")
            
            org_urn_display.change(
                fn=initial_load_sequence,
                inputs=[url_user_token_display, org_urn_display, token_state],
                outputs=[status_box, token_state, sync_data_btn, dashboard_display_html],
                show_progress="full"
            )

        with gr.TabItem("2️⃣ Analisi", id="tab_analytics"):
            gr.Markdown("## 📈 Analisi Performance LinkedIn")
            gr.Markdown("Seleziona un intervallo di date. Clicca i pulsanti (💣 Insights, ƒ Formula, 🧭 Esplora) su un grafico per azioni.")
            analytics_status_md = gr.Markdown("Stato analisi...")
            
            with gr.Row():
                date_filter_selector = gr.Radio(
                    ["Sempre", "Ultimi 7 Giorni", "Ultimi 30 Giorni", "Intervallo Personalizzato"],
                    label="Seleziona Intervallo Date", value="Sempre", scale=3
                )
                with gr.Column(scale=2): 
                    custom_start_date_picker = gr.DateTime(label="Data Inizio", visible=False, include_time=False, type="datetime") # Use gr.DateTime
                    custom_end_date_picker = gr.DateTime(label="Data Fine", visible=False, include_time=False, type="datetime") # Use gr.DateTime

            apply_filter_btn = gr.Button("🔍 Applica Filtro & Aggiorna Analisi", variant="primary")

            def toggle_custom_date_pickers(selection):
                is_custom = selection == "Intervallo Personalizzato"
                return gr.update(visible=is_custom), gr.update(visible=is_custom)

            date_filter_selector.change(
                fn=toggle_custom_date_pickers,
                inputs=[date_filter_selector],
                outputs=[custom_start_date_picker, custom_end_date_picker]
            )
            
            plot_configs = [
                {"label": "Numero di Follower nel Tempo", "id": "followers_count", "section": "Dinamiche dei Follower"},
                {"label": "Tasso di Crescita Follower", "id": "followers_growth_rate", "section": "Dinamiche dei Follower"},
                {"label": "Follower per Località", "id": "followers_by_location", "section": "Demografia Follower"},
                {"label": "Follower per Ruolo (Funzione)", "id": "followers_by_role", "section": "Demografia Follower"},
                {"label": "Follower per Settore", "id": "followers_by_industry", "section": "Demografia Follower"},
                {"label": "Follower per Anzianità", "id": "followers_by_seniority", "section": "Demografia Follower"},
                {"label": "Tasso di Engagement nel Tempo", "id": "engagement_rate", "section": "Approfondimenti Performance Post"},
                {"label": "Copertura nel Tempo", "id": "reach_over_time", "section": "Approfondimenti Performance Post"},
                {"label": "Visualizzazioni nel Tempo", "id": "impressions_over_time", "section": "Approfondimenti Performance Post"},
                {"label": "Reazioni (Like) nel Tempo", "id": "likes_over_time", "section": "Approfondimenti Performance Post"},
                {"label": "Click nel Tempo", "id": "clicks_over_time", "section": "Engagement Dettagliato Post nel Tempo"},
                {"label": "Condivisioni nel Tempo", "id": "shares_over_time", "section": "Engagement Dettagliato Post nel Tempo"},
                {"label": "Commenti nel Tempo", "id": "comments_over_time", "section": "Engagement Dettagliato Post nel Tempo"},
                {"label": "Ripartizione Commenti per Sentiment", "id": "comments_sentiment", "section": "Engagement Dettagliato Post nel Tempo"},
                {"label": "Frequenza Post", "id": "post_frequency_cs", "section": "Analisi Strategia Contenuti"},
                {"label": "Ripartizione Contenuti per Formato", "id": "content_format_breakdown_cs", "section": "Analisi Strategia Contenuti"},
                {"label": "Ripartizione Contenuti per Argomenti", "id": "content_topic_breakdown_cs", "section": "Analisi Strategia Contenuti"},
                {"label": "Volume Menzioni nel Tempo (Dettaglio)", "id": "mention_analysis_volume", "section": "Analisi Menzioni (Dettaglio)"},
                {"label": "Ripartizione Menzioni per Sentiment (Dettaglio)", "id": "mention_analysis_sentiment", "section": "Analisi Menzioni (Dettaglio)"}
            ]
            assert len(plot_configs) == 19, "Mancata corrispondenza in plot_configs e grafici attesi."

            active_panel_action_state = gr.State(None) 
            explored_plot_id_state = gr.State(None) 
            
            plot_ui_objects = {} 

            with gr.Row(equal_height=False):
                with gr.Column(scale=8) as plots_area_col:
                    plot_ui_objects = build_analytics_tab_plot_area(plot_configs)
                
                with gr.Column(scale=4, visible=False) as global_actions_column_ui: 
                    gr.Markdown("### 💡 Azioni Contestuali Grafico") 
                    
                    insights_chatbot_ui = gr.Chatbot(
                        label="Chat Insights", type="messages", height=450, 
                        bubble_full_width=False, visible=False, show_label=False,
                        placeholder="L'analisi AI del grafico apparirà qui. Fai domande di approfondimento!"
                    )
                    insights_chat_input_ui = gr.Textbox(
                        label="La tua domanda:", placeholder="Chiedi all'AI riguardo a questo grafico...", 
                        lines=2, visible=False, show_label=False
                    )
                    with gr.Row(visible=False) as insights_suggestions_row_ui:
                        insights_suggestion_1_btn = gr.Button(value="Suggerimento 1", size="sm", min_width=50)
                        insights_suggestion_2_btn = gr.Button(value="Suggerimento 2", size="sm", min_width=50)
                        insights_suggestion_3_btn = gr.Button(value="Suggerimento 3", size="sm", min_width=50)
                    
                    formula_display_markdown_ui = gr.Markdown(
                        "I dettagli sulla formula/metodologia appariranno qui.", visible=False
                    )

            async def handle_panel_action( 
                plot_id_clicked: str, 
                action_type: str, 
                current_active_action_from_state: dict, 
                current_chat_histories: dict, 
                current_chat_plot_id: str,
                current_plot_data_for_chatbot: dict # NEW: data summaries
            ):
                logging.info(f"Azione '{action_type}' per grafico: {plot_id_clicked}. Attualmente attivo: {current_active_action_from_state}")
                
                clicked_plot_config = next((p for p in plot_configs if p["id"] == plot_id_clicked), None)
                if not clicked_plot_config:
                    logging.error(f"Configurazione non trovata per plot_id {plot_id_clicked}")
                    num_button_updates = 2 * len(plot_configs) # insights, formula buttons
                    error_updates = [gr.update(visible=False)] * 7 # action_col, chatbot, input, suggestions_row, 3x sugg_btn
                    error_updates.append(gr.update(visible=False, value="")) # formula_md (visibility and value)
                    error_updates.extend([current_active_action_from_state, current_chat_plot_id, current_chat_histories]) 
                    error_updates.extend([gr.update()] * num_button_updates) 
                    return error_updates

                clicked_plot_label = clicked_plot_config["label"]
                
                hypothetical_new_active_state = {"plot_id": plot_id_clicked, "type": action_type}
                is_toggling_off = current_active_action_from_state == hypothetical_new_active_state
                
                new_active_action_state_to_set = None
                action_col_visible_update = gr.update(visible=True)
                
                insights_chatbot_visible_update = gr.update(visible=False)
                insights_chat_input_visible_update = gr.update(visible=False)
                insights_suggestions_row_visible_update = gr.update(visible=False)
                formula_display_visible_update = gr.update(visible=False)
                
                chatbot_content_update = gr.update() # No change by default
                suggestion_1_update = gr.update()
                suggestion_2_update = gr.update()
                suggestion_3_update = gr.update()
                new_current_chat_plot_id = current_chat_plot_id 
                updated_chat_histories = current_chat_histories 

                formula_content_update = gr.update()

                if is_toggling_off:
                    new_active_action_state_to_set = None
                    action_col_visible_update = gr.update(visible=False)
                    new_current_chat_plot_id = None 
                    logging.info(f"Chiusura pannello {action_type} per {plot_id_clicked}")
                else:
                    new_active_action_state_to_set = hypothetical_new_active_state
                    if action_type == "insights":
                        insights_chatbot_visible_update = gr.update(visible=True)
                        insights_chat_input_visible_update = gr.update(visible=True)
                        insights_suggestions_row_visible_update = gr.update(visible=True)
                        
                        new_current_chat_plot_id = plot_id_clicked
                        chat_history_for_this_plot = current_chat_histories.get(plot_id_clicked, [])
                        
                        plot_specific_data_summary = current_plot_data_for_chatbot.get(plot_id_clicked, f"Nessun sommario dati specifico disponibile per '{clicked_plot_label}'.")

                        if not chat_history_for_this_plot: 
                            initial_llm_prompt, suggestions = get_initial_insight_prompt_and_suggestions(
                                plot_id_clicked, 
                                clicked_plot_label,
                                plot_specific_data_summary 
                            )
                            # History for LLM's first turn: the system's prompt as a user message
                            history_for_llm_first_turn = [{"role": "user", "content": initial_llm_prompt}]
                            
                            logging.info(f"Generating initial LLM insight for {plot_id_clicked}...")
                            initial_bot_response_text = await generate_llm_response(
                                user_message=initial_llm_prompt, # For context/logging in handler
                                plot_id=plot_id_clicked, 
                                plot_label=clicked_plot_label, 
                                chat_history_for_plot=history_for_llm_first_turn, 
                                plot_data_summary=plot_specific_data_summary 
                            )
                            logging.info(f"LLM initial insight received for {plot_id_clicked}.")
                            
                            # History for Gradio display starts with the assistant's response
                            chat_history_for_this_plot = [{"role": "assistant", "content": initial_bot_response_text}]
                            updated_chat_histories = current_chat_histories.copy()
                            updated_chat_histories[plot_id_clicked] = chat_history_for_this_plot
                        else: # History exists, get fresh suggestions
                            _, suggestions = get_initial_insight_prompt_and_suggestions(
                                plot_id_clicked, 
                                clicked_plot_label,
                                plot_specific_data_summary
                            )

                        chatbot_content_update = gr.update(value=chat_history_for_this_plot)
                        suggestion_1_update = gr.update(value=suggestions[0])
                        suggestion_2_update = gr.update(value=suggestions[1])
                        suggestion_3_update = gr.update(value=suggestions[2])
                        logging.info(f"Apertura pannello CHAT per {plot_id_clicked} ('{clicked_plot_label}')")

                    elif action_type == "formula":
                        formula_display_visible_update = gr.update(visible=True)
                        formula_key = PLOT_ID_TO_FORMULA_KEY_MAP.get(plot_id_clicked)
                        formula_text = f"**Formula/Metodologia per: {clicked_plot_label}**\n\nID Grafico: `{plot_id_clicked}`.\n\n"
                        if formula_key and formula_key in PLOT_FORMULAS:
                            formula_data = PLOT_FORMULAS[formula_key]
                            formula_text += f"### {formula_data['title']}\n\n"
                            formula_text += f"**Descrizione:**\n{formula_data['description']}\n\n"
                            formula_text += "**Come viene calcolato:**\n"
                            for step in formula_data['calculation_steps']:
                                formula_text += f"- {step}\n"
                        else:
                            formula_text += "(Nessuna informazione dettagliata sulla formula trovata per questo ID grafico in `formulas.py`)"
                        formula_content_update = gr.update(value=formula_text)
                        new_current_chat_plot_id = None 
                        logging.info(f"Apertura pannello FORMULA per {plot_id_clicked} (mappato a {formula_key})")
                
                all_button_icon_updates = []
                for cfg_item in plot_configs:
                    p_id_iter = cfg_item["id"]
                    # Update insights button icon
                    if new_active_action_state_to_set == {"plot_id": p_id_iter, "type": "insights"}:
                        all_button_icon_updates.append(gr.update(value=ACTIVE_ICON))
                    else:
                        all_button_icon_updates.append(gr.update(value=BOMB_ICON))
                    # Update formula button icon
                    if new_active_action_state_to_set == {"plot_id": p_id_iter, "type": "formula"}:
                        all_button_icon_updates.append(gr.update(value=ACTIVE_ICON))
                    else:
                        all_button_icon_updates.append(gr.update(value=FORMULA_ICON))
                
                final_updates = [
                    action_col_visible_update,
                    insights_chatbot_visible_update, chatbot_content_update,
                    insights_chat_input_visible_update,
                    insights_suggestions_row_visible_update, suggestion_1_update, suggestion_2_update, suggestion_3_update,
                    formula_display_visible_update, formula_content_update,
                    new_active_action_state_to_set, 
                    new_current_chat_plot_id,  
                    updated_chat_histories   
                ] + all_button_icon_updates
                
                return final_updates

            async def handle_chat_message_submission(
                user_message: str, 
                current_plot_id: str, 
                chat_histories: dict, 
                current_plot_data_for_chatbot: dict # NEW: data summaries
            ):
                if not current_plot_id or not user_message.strip():
                    history_for_plot = chat_histories.get(current_plot_id, [])
                    # Yield current state if no action needed
                    yield history_for_plot, gr.update(value=""), chat_histories # Clear input, return current history
                    return

                plot_config = next((p for p in plot_configs if p["id"] == current_plot_id), None)
                plot_label = plot_config["label"] if plot_config else "Grafico Selezionato"
                
                # Retrieve the specific data summary for the current plot
                plot_specific_data_summary = current_plot_data_for_chatbot.get(current_plot_id, f"Nessun sommario dati specifico disponibile per '{plot_label}'.")

                history_for_plot = chat_histories.get(current_plot_id, []).copy()
                history_for_plot.append({"role": "user", "content": user_message})
                
                # Update UI immediately with user message
                yield history_for_plot, gr.update(value=""), chat_histories # Clear input

                # Pass the data summary to the LLM along with the history
                bot_response_text = await generate_llm_response(
                    user_message, 
                    current_plot_id, 
                    plot_label, 
                    history_for_plot, # This history now includes the user message
                    plot_specific_data_summary # Explicitly pass for this turn if needed by LLM handler logic
                )
                
                history_for_plot.append({"role": "assistant", "content": bot_response_text})
                
                updated_chat_histories = chat_histories.copy()
                updated_chat_histories[current_plot_id] = history_for_plot
                
                yield history_for_plot, "", updated_chat_histories


            async def handle_suggested_question_click(
                suggestion_text: str,
                current_plot_id: str, 
                chat_histories: dict, 
                current_plot_data_for_chatbot: dict # NEW: data summaries
            ):
                if not current_plot_id or not suggestion_text.strip():
                    history_for_plot = chat_histories.get(current_plot_id, [])
                    yield history_for_plot, gr.update(value=""), chat_histories 
                    return

                # This is essentially the same as submitting a message, so reuse logic
                # The suggestion_text becomes the user_message
                async for update in handle_chat_message_submission(
                    suggestion_text, 
                    current_plot_id, 
                    chat_histories, 
                    current_plot_data_for_chatbot
                ):
                    yield update


            def handle_explore_click(plot_id_clicked, current_explored_plot_id_from_state):
                logging.info(f"Click su Esplora per: {plot_id_clicked}. Attualmente esplorato da stato: {current_explored_plot_id_from_state}")
                if not plot_ui_objects:
                    logging.error("plot_ui_objects non popolato durante handle_explore_click.")
                    updates_for_missing_ui = [current_explored_plot_id_from_state] 
                    for _ in plot_configs: # panel_component, explore_button
                        updates_for_missing_ui.extend([gr.update(), gr.update()]) 
                    return updates_for_missing_ui
                
                new_explored_id_to_set = None
                is_toggling_off = (plot_id_clicked == current_explored_plot_id_from_state)
                
                if is_toggling_off:
                    new_explored_id_to_set = None
                    logging.info(f"Interruzione esplorazione grafico: {plot_id_clicked}")
                else:
                    new_explored_id_to_set = plot_id_clicked
                    logging.info(f"Esplorazione grafico: {plot_id_clicked}")
                                        
                panel_and_button_updates = []
                for cfg in plot_configs:
                    p_id = cfg["id"]
                    if p_id in plot_ui_objects:
                        panel_visible = not new_explored_id_to_set or (p_id == new_explored_id_to_set)
                        panel_and_button_updates.append(gr.update(visible=panel_visible)) 
                                                
                        if p_id == new_explored_id_to_set: 
                            panel_and_button_updates.append(gr.update(value=ACTIVE_ICON))
                        else:
                            panel_and_button_updates.append(gr.update(value=EXPLORE_ICON))
                    else: 
                        panel_and_button_updates.extend([gr.update(), gr.update()])
                                        
                final_updates = [new_explored_id_to_set] + panel_and_button_updates 
                return final_updates
            
            # Outputs for panel actions
            action_panel_outputs_list = [
                global_actions_column_ui, 
                insights_chatbot_ui, insights_chatbot_ui, # Target chatbot UI for visibility and value
                insights_chat_input_ui, 
                insights_suggestions_row_ui, insights_suggestion_1_btn, insights_suggestion_2_btn, insights_suggestion_3_btn, 
                formula_display_markdown_ui, formula_display_markdown_ui, # Target markdown for visibility and value
                active_panel_action_state, 
                current_chat_plot_id_st,
                chat_histories_st
            ]
            for cfg_item_action in plot_configs: 
                pid_action = cfg_item_action["id"]
                if pid_action in plot_ui_objects:
                    action_panel_outputs_list.append(plot_ui_objects[pid_action]["bomb_button"])
                    action_panel_outputs_list.append(plot_ui_objects[pid_action]["formula_button"])
                else: 
                    action_panel_outputs_list.extend([gr.update(), gr.update()]) # Use gr.update() as placeholder

            # Outputs for explore actions
            explore_buttons_outputs_list = [explored_plot_id_state]
            for cfg_item_explore in plot_configs:
                pid_explore = cfg_item_explore["id"]
                if pid_explore in plot_ui_objects:
                    explore_buttons_outputs_list.append(plot_ui_objects[pid_explore]["panel_component"])
                    explore_buttons_outputs_list.append(plot_ui_objects[pid_explore]["explore_button"])
                else: 
                    explore_buttons_outputs_list.extend([gr.update(), gr.update()])

            # Inputs for panel actions
            action_click_inputs = [
                active_panel_action_state, 
                chat_histories_st,
                current_chat_plot_id_st,
                plot_data_for_chatbot_st # NEW: pass data summaries state
            ]
            # Inputs for explore actions
            explore_click_inputs = [explored_plot_id_state]

            def create_panel_action_handler(p_id, action_type_str):
                async def _handler(current_active_val, current_chats_val, current_chat_pid, current_plot_data_summaries): # Add summaries
                    logging.debug(f"Entering _handler for plot_id: {p_id}, action: {action_type_str}")
                    result = await handle_panel_action(p_id, action_type_str, current_active_val, current_chats_val, current_chat_pid, current_plot_data_summaries) # Pass summaries
                    logging.debug(f"_handler for plot_id: {p_id}, action: {action_type_str} completed.")
                    return result
                return _handler

            for config_item in plot_configs:
                plot_id = config_item["id"]
                if plot_id in plot_ui_objects:
                    ui_obj = plot_ui_objects[plot_id]
                    
                    ui_obj["bomb_button"].click(
                        fn=create_panel_action_handler(plot_id, "insights"), 
                        inputs=action_click_inputs, 
                        outputs=action_panel_outputs_list,
                        api_name=f"action_insights_{plot_id}"
                    )
                    ui_obj["formula_button"].click(
                        fn=create_panel_action_handler(plot_id, "formula"), 
                        inputs=action_click_inputs,
                        outputs=action_panel_outputs_list,
                        api_name=f"action_formula_{plot_id}"
                    )
                    ui_obj["explore_button"].click(
                        fn=lambda current_explored_val, p_id=plot_id: handle_explore_click(p_id, current_explored_val),
                        inputs=explore_click_inputs,
                        outputs=explore_buttons_outputs_list,
                        api_name=f"action_explore_{plot_id}"
                    )
                else:
                    logging.warning(f"Oggetto UI per plot_id '{plot_id}' non trovato durante il tentativo di associare i gestori di click.")
            
            chat_submission_outputs = [insights_chatbot_ui, insights_chat_input_ui, chat_histories_st]
            chat_submission_inputs = [insights_chat_input_ui, current_chat_plot_id_st, chat_histories_st, plot_data_for_chatbot_st] # Add data summaries state

            insights_chat_input_ui.submit(
                fn=handle_chat_message_submission,
                inputs=chat_submission_inputs, 
                outputs=chat_submission_outputs,
                api_name="submit_chat_message"
            )

            suggestion_click_inputs = [current_chat_plot_id_st, chat_histories_st, plot_data_for_chatbot_st] # Add data summaries state
            insights_suggestion_1_btn.click(
                fn=handle_suggested_question_click,
                inputs=[insights_suggestion_1_btn] + suggestion_click_inputs, # Pass button value as first arg
                outputs=chat_submission_outputs, 
                api_name="click_suggestion_1"
            )
            insights_suggestion_2_btn.click(
                fn=handle_suggested_question_click,
                inputs=[insights_suggestion_2_btn] + suggestion_click_inputs, 
                outputs=chat_submission_outputs,
                api_name="click_suggestion_2"
            )
            insights_suggestion_3_btn.click(
                fn=handle_suggested_question_click,
                inputs=[insights_suggestion_3_btn] + suggestion_click_inputs, 
                outputs=chat_submission_outputs,
                api_name="click_suggestion_3"
            )

            def refresh_all_analytics_ui_elements(current_token_state, date_filter_val, custom_start_val, custom_end_val, current_chat_histories):
                logging.info("Aggiornamento di tutti gli elementi UI delle analisi e reset delle azioni/chat.")
                
                # Pass plot_configs to the update function so it can be used by generate_chatbot_data_summaries
                plot_generation_results = update_analytics_plots_figures(
                    current_token_state, date_filter_val, custom_start_val, custom_end_val, plot_configs
                )
                status_message_update = plot_generation_results[0]
                generated_plot_figures = plot_generation_results[1:-1] # All items except first (status) and last (summaries)
                new_plot_data_summaries = plot_generation_results[-1] # Last item is the summaries dict
                
                all_updates = [status_message_update] 
                
                for i in range(len(plot_configs)): 
                    if i < len(generated_plot_figures):
                        all_updates.append(generated_plot_figures[i]) 
                    else: 
                        all_updates.append(create_placeholder_plot("Errore Figura", f"Figura mancante per grafico {plot_configs[i]['id']}"))
                
                all_updates.extend([
                    gr.update(visible=False), # global_actions_column_ui
                    gr.update(value=[], visible=False), # insights_chatbot_ui (value & visibility)
                    gr.update(value="", visible=False), # insights_chat_input_ui (value & visibility)
                    gr.update(visible=False), # insights_suggestions_row_ui
                    gr.update(value="Suggerimento 1"), # insights_suggestion_1_btn (reset value, visibility handled by row)
                    gr.update(value="Suggerimento 2"), # insights_suggestion_2_btn
                    gr.update(value="Suggerimento 3"), # insights_suggestion_3_btn
                    gr.update(value="I dettagli sulla formula/metodologia appariranno qui.", visible=False), # formula_display_markdown_ui
                    None, # active_panel_action_state
                    None, # current_chat_plot_id_st
                    {}, # chat_histories_st (reset chat histories on filter change)
                    new_plot_data_summaries # NEW: plot_data_for_chatbot_st
                ])
                
                for cfg in plot_configs: 
                    pid = cfg["id"]
                    if pid in plot_ui_objects:
                        all_updates.append(gr.update(value=BOMB_ICON))   
                        all_updates.append(gr.update(value=FORMULA_ICON)) 
                        all_updates.append(gr.update(value=EXPLORE_ICON)) 
                        all_updates.append(gr.update(visible=True))     # panel_component visibility
                    else: 
                        all_updates.extend([gr.update(), gr.update(), gr.update(), gr.update()])
                
                all_updates.append(None) # explored_plot_id_state
                
                logging.info(f"Preparati {len(all_updates)} aggiornamenti per il refresh delle analisi.")
                return all_updates

            apply_filter_and_sync_outputs_list = [analytics_status_md] 
            for config_item_filter_sync in plot_configs:
                pid_filter_sync = config_item_filter_sync["id"]
                if pid_filter_sync in plot_ui_objects and "plot_component" in plot_ui_objects[pid_filter_sync]:
                    apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync]["plot_component"])
                else:
                    apply_filter_and_sync_outputs_list.append(gr.update()) 
            
            apply_filter_and_sync_outputs_list.extend([
                global_actions_column_ui,       # Reset visibility
                insights_chatbot_ui,            # Reset content & visibility
                insights_chat_input_ui,         # Reset content & visibility
                insights_suggestions_row_ui,    # Reset visibility
                insights_suggestion_1_btn,      # Reset text & visibility
                insights_suggestion_2_btn,
                insights_suggestion_3_btn,
                formula_display_markdown_ui,    # Reset content & visibility
                active_panel_action_state,      # Reset state
                current_chat_plot_id_st,        # Reset state
                chat_histories_st,              # Preserve or reset state (resetting via refresh_all_analytics_ui_elements)
                plot_data_for_chatbot_st        # NEW: Update this state
            ])

            for cfg_filter_sync_btns in plot_configs:
                pid_filter_sync_btns = cfg_filter_sync_btns["id"]
                if pid_filter_sync_btns in plot_ui_objects:
                    apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync_btns]["bomb_button"])
                    apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync_btns]["formula_button"])
                    apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync_btns]["explore_button"])
                    apply_filter_and_sync_outputs_list.append(plot_ui_objects[pid_filter_sync_btns]["panel_component"]) 
                else:
                    apply_filter_and_sync_outputs_list.extend([gr.update(), gr.update(), gr.update(), gr.update()]) 
            
            apply_filter_and_sync_outputs_list.append(explored_plot_id_state) # Reset state
            
            logging.info(f"Output totali definiti per apply_filter/sync: {len(apply_filter_and_sync_outputs_list)}")
            
            apply_filter_btn.click(
                fn=refresh_all_analytics_ui_elements,
                inputs=[token_state, date_filter_selector, custom_start_date_picker, custom_end_date_picker, chat_histories_st],
                outputs=apply_filter_and_sync_outputs_list,
                show_progress="full"
            )

        with gr.TabItem("3️⃣ Menzioni", id="tab_mentions"):
            refresh_mentions_display_btn = gr.Button("🔄 Aggiorna Visualizzazione Menzioni", variant="secondary")
            mentions_html = gr.HTML("Dati menzioni...")
            mentions_sentiment_dist_plot = gr.Plot(label="Distribuzione Sentiment Menzioni")
            refresh_mentions_display_btn.click(
                fn=run_mentions_tab_display, inputs=[token_state],
                outputs=[mentions_html, mentions_sentiment_dist_plot],
                show_progress="full"
            )

        with gr.TabItem("4️⃣ Statistiche Follower", id="tab_follower_stats"):
            refresh_follower_stats_btn = gr.Button("🔄 Aggiorna Visualizzazione Statistiche Follower", variant="secondary")
            follower_stats_html = gr.HTML("Statistiche follower...")
            with gr.Row(): 
                fs_plot_monthly_gains = gr.Plot(label="Guadagni Mensili Follower")
            with gr.Row():
                fs_plot_seniority = gr.Plot(label="Follower per Anzianità (Top 10 Organici)")
                fs_plot_industry = gr.Plot(label="Follower per Settore (Top 10 Organici)")
            
            refresh_follower_stats_btn.click(
                fn=run_follower_stats_tab_display, inputs=[token_state],
                outputs=[follower_stats_html, fs_plot_monthly_gains, fs_plot_seniority, fs_plot_industry],
                show_progress="full"
            )
            
    sync_event_part1 = sync_data_btn.click(
        fn=sync_all_linkedin_data_orchestrator,
        inputs=[token_state], outputs=[sync_status_html_output, token_state], show_progress="full"
    )
    sync_event_part2 = sync_event_part1.then( 
        fn=process_and_store_bubble_token, 
        inputs=[url_user_token_display, org_urn_display, token_state],
        outputs=[status_box, token_state, sync_data_btn], show_progress=False 
    )
    sync_event_part3 = sync_event_part2.then(
        fn=display_main_dashboard, 
        inputs=[token_state], outputs=[dashboard_display_html], show_progress=False
    )
    sync_event_final = sync_event_part3.then(
        fn=refresh_all_analytics_ui_elements, # This will now also update chatbot data summaries
        inputs=[token_state, date_filter_selector, custom_start_date_picker, custom_end_date_picker, chat_histories_st],
        outputs=apply_filter_and_sync_outputs_list, 
        show_progress="full"
    )

if __name__ == "__main__":
    if not os.environ.get(LINKEDIN_CLIENT_ID_ENV_VAR):
        logging.warning(f"ATTENZIONE: Variabile d'ambiente '{LINKEDIN_CLIENT_ID_ENV_VAR}' non impostata.")
    if not os.environ.get(BUBBLE_APP_NAME_ENV_VAR) or \
       not os.environ.get(BUBBLE_API_KEY_PRIVATE_ENV_VAR) or \
       not os.environ.get(BUBBLE_API_ENDPOINT_ENV_VAR):
        logging.warning("ATTENZIONE: Variabili d'ambiente Bubble non completamente impostate.")
    
    try:
        logging.info(f"Versione Matplotlib: {matplotlib.__version__}, Backend: {matplotlib.get_backend()}")
    except ImportError: 
        logging.warning("Matplotlib non trovato direttamente, ma potrebbe essere usato dai generatori di grafici.")
        
    app.launch(server_name="0.0.0.0", server_port=7860, debug=True)