File size: 53,903 Bytes
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d568e3b
 
824bf31
3c90182
 
 
 
824bf31
 
 
c78f128
3c90182
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c78f128
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d568e3b
 
 
 
 
 
 
 
 
 
c78f128
 
d568e3b
 
 
 
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c78f128
 
 
824bf31
 
 
 
 
 
 
 
 
 
 
 
d568e3b
824bf31
 
 
 
d568e3b
 
 
 
 
 
 
 
 
824bf31
 
 
d568e3b
824bf31
d568e3b
824bf31
 
 
 
 
 
d568e3b
824bf31
3c90182
 
824bf31
 
 
 
d568e3b
 
824bf31
 
 
d568e3b
 
3c90182
 
 
 
 
 
 
 
824bf31
 
d568e3b
 
824bf31
 
 
 
 
3c90182
 
 
 
 
 
 
824bf31
d568e3b
 
 
 
824bf31
 
 
 
 
 
 
 
c78f128
 
 
 
 
 
 
824bf31
 
 
 
 
 
 
3c90182
 
 
 
 
 
 
 
824bf31
 
 
 
 
 
 
 
 
 
 
 
d568e3b
824bf31
 
 
 
 
3c90182
 
 
 
 
 
 
 
 
 
 
 
 
824bf31
 
 
 
 
3c90182
824bf31
 
d568e3b
 
 
 
824bf31
 
 
 
3c90182
 
 
 
 
 
 
824bf31
 
 
 
d568e3b
824bf31
 
d568e3b
 
 
 
 
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c90182
 
 
 
 
 
 
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
3c90182
 
 
 
 
 
 
824bf31
 
 
 
 
 
 
 
 
 
c78f128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
824bf31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
"""
Module: agents.zumbi
Codinome: Zumbi - Investigador de Padrões
Description: Agent specialized in detecting anomalies and suspicious patterns in government data
Author: Anderson H. Silva
Date: 2025-01-24
License: Proprietary - All rights reserved
"""

import asyncio
from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Tuple
from dataclasses import dataclass

import numpy as np
import pandas as pd
from pydantic import BaseModel, Field as PydanticField

from src.agents.deodoro import BaseAgent, AgentContext, AgentMessage, AgentResponse
from src.core import get_logger, AgentStatus
from src.core.exceptions import AgentExecutionError, DataAnalysisError
from src.core.monitoring import (
    INVESTIGATIONS_TOTAL, ANOMALIES_DETECTED, INVESTIGATION_DURATION,
    DATA_RECORDS_PROCESSED, TRANSPARENCY_API_DATA_FETCHED
)
from src.tools.transparency_api import TransparencyAPIClient, TransparencyAPIFilter
from src.tools.models_client import ModelsClient, get_models_client
from src.ml.spectral_analyzer import SpectralAnalyzer, SpectralAnomaly
from src.tools.dados_gov_tool import DadosGovTool
import time


@dataclass
class AnomalyResult:
    """Result of anomaly detection analysis."""
    
    anomaly_type: str
    severity: float  # 0.0 to 1.0
    confidence: float  # 0.0 to 1.0
    description: str
    explanation: str
    evidence: Dict[str, Any]
    recommendations: List[str]
    affected_entities: List[Dict[str, Any]]
    financial_impact: Optional[float] = None


class InvestigationRequest(BaseModel):
    """Request for investigation with specific parameters."""
    
    query: str = PydanticField(description="Natural language investigation query")
    organization_codes: Optional[List[str]] = PydanticField(default=None, description="Specific organization codes to investigate")
    date_range: Optional[Tuple[str, str]] = PydanticField(default=None, description="Date range (start, end) in DD/MM/YYYY format")
    value_threshold: Optional[float] = PydanticField(default=None, description="Minimum value threshold for contracts")
    anomaly_types: Optional[List[str]] = PydanticField(default=None, description="Specific types of anomalies to look for")
    max_records: int = PydanticField(default=100, description="Maximum records to analyze")
    enable_open_data_enrichment: bool = PydanticField(default=True, description="Enable enrichment with dados.gov.br open data")


class InvestigatorAgent(BaseAgent):
    """
    Agent specialized in detecting anomalies and suspicious patterns in government data.
    
    Capabilities:
    - Price anomaly detection (overpriced contracts)
    - Temporal pattern analysis (suspicious timing)
    - Vendor concentration analysis (monopolization)
    - Duplicate contract detection
    - Unusual payment patterns
    - Explainable AI for transparency
    """
    
    def __init__(
        self,
        price_anomaly_threshold: float = 2.5,  # Standard deviations
        concentration_threshold: float = 0.7,   # 70% concentration trigger
        duplicate_similarity_threshold: float = 0.85,  # 85% similarity
    ):
        """
        Initialize the Investigator Agent.
        
        Args:
            price_anomaly_threshold: Number of standard deviations for price anomalies
            concentration_threshold: Threshold for vendor concentration (0-1)
            duplicate_similarity_threshold: Threshold for duplicate detection (0-1)
        """
        super().__init__(
            name="Zumbi",
            description="Zumbi dos Palmares - Agent specialized in detecting anomalies and suspicious patterns in government data",
            capabilities=[
                "price_anomaly_detection",
                "temporal_pattern_analysis",
                "vendor_concentration_analysis",
                "duplicate_contract_detection",
                "payment_pattern_analysis",
                "spectral_analysis",
                "explainable_ai",
                "open_data_enrichment"
            ],
            max_retries=3,
            timeout=60
        )
        self.price_threshold = price_anomaly_threshold
        self.concentration_threshold = concentration_threshold
        self.duplicate_threshold = duplicate_similarity_threshold
        
        # Initialize models client for ML inference (only if enabled)
        from src.core import settings
        if settings.models_api_enabled:
            self.models_client = get_models_client()
        else:
            self.models_client = None
            self.logger.info("Models API disabled, using only local ML")
        
        # Initialize spectral analyzer for frequency-domain analysis (fallback)
        self.spectral_analyzer = SpectralAnalyzer()
        
        # Initialize dados.gov.br tool for accessing open data
        self.dados_gov_tool = DadosGovTool()
        
        # Anomaly detection methods registry
        self.anomaly_detectors = {
            "price_anomaly": self._detect_price_anomalies,
            "vendor_concentration": self._detect_vendor_concentration,
            "temporal_patterns": self._detect_temporal_anomalies,
            "spectral_patterns": self._detect_spectral_anomalies,
            "duplicate_contracts": self._detect_duplicate_contracts,
            "payment_patterns": self._detect_payment_anomalies,
        }
        
        self.logger.info(
            "zumbi_initialized",
            agent_name=self.name,
            price_threshold=price_anomaly_threshold,
            concentration_threshold=concentration_threshold,
        )
    
    async def initialize(self) -> None:
        """Initialize agent resources."""
        self.logger.info(f"{self.name} agent initialized")
    
    async def shutdown(self) -> None:
        """Cleanup agent resources."""
        self.logger.info(f"{self.name} agent shutting down")
    
    async def process(
        self,
        message: AgentMessage,
        context: AgentContext
    ) -> AgentResponse:
        """
        Process investigation request and return anomaly detection results.
        
        Args:
            message: Investigation request message
            context: Agent execution context
            
        Returns:
            AgentResponse with detected anomalies
        """
        investigation_start_time = time.time()
        
        try:
            self.logger.info(
                "investigation_started",
                investigation_id=context.investigation_id,
                agent_name=self.name,
                action=message.action,
            )
            
            # Parse investigation request
            if message.action == "investigate":
                request = InvestigationRequest(**message.payload)
                
                # Record investigation start
                INVESTIGATIONS_TOTAL.labels(
                    agent_type="zumbi",
                    investigation_type=request.anomaly_types[0] if request.anomaly_types else "general",
                    status="started"
                ).inc()
                
            else:
                raise AgentExecutionError(
                    f"Unsupported action: {message.action}",
                    agent_id=self.name
                )
            
            # Fetch data for investigation
            contracts_data = await self._fetch_investigation_data(request, context)
            
            # Record data processed
            DATA_RECORDS_PROCESSED.labels(
                data_source="transparency_api",
                agent="zumbi",
                operation="fetch"
            ).inc(len(contracts_data) if contracts_data else 0)
            
            if not contracts_data:
                return AgentResponse(
                    agent_name=self.name,
                    status=AgentStatus.COMPLETED,
                    result={
                        "status": "no_data",
                        "message": "No data found for the specified criteria",
                        "anomalies": [],
                        "summary": {"total_records": 0, "anomalies_found": 0}
                    },
                    metadata={"investigation_id": context.investigation_id}
                )
            
            # Enrich data with open data information if available
            if request.enable_open_data_enrichment:
                contracts_data = await self._enrich_with_open_data(
                    contracts_data,
                    context
                )
            
            # Run anomaly detection
            anomalies = await self._run_anomaly_detection(
                contracts_data, 
                request, 
                context
            )
            
            # Record anomalies detected
            for anomaly in anomalies:
                ANOMALIES_DETECTED.labels(
                    anomaly_type=anomaly.anomaly_type,
                    severity="high" if anomaly.severity > 0.7 else "medium" if anomaly.severity > 0.4 else "low",
                    agent="zumbi"
                ).inc()
            
            # Generate investigation summary
            summary = self._generate_investigation_summary(contracts_data, anomalies)
            
            # Create result message
            result = {
                "status": "completed",
                "query": request.query,
                "anomalies": [self._anomaly_to_dict(a) for a in anomalies],
                "summary": summary,
                "metadata": {
                    "investigation_id": context.investigation_id,
                    "timestamp": datetime.utcnow().isoformat(),
                    "agent_name": self.name,
                    "records_analyzed": len(contracts_data),
                    "anomalies_detected": len(anomalies),
                }
            }
            
            # Record investigation completion and duration
            investigation_duration = time.time() - investigation_start_time
            INVESTIGATION_DURATION.labels(
                agent_type="zumbi",
                investigation_type=request.anomaly_types[0] if request.anomaly_types else "general"
            ).observe(investigation_duration)
            
            INVESTIGATIONS_TOTAL.labels(
                agent_type="zumbi",
                investigation_type=request.anomaly_types[0] if request.anomaly_types else "general",
                status="completed"
            ).inc()
            
            self.logger.info(
                "investigation_completed",
                investigation_id=context.investigation_id,
                records_analyzed=len(contracts_data),
                anomalies_found=len(anomalies),
                duration_seconds=investigation_duration,
            )
            
            return AgentResponse(
                agent_name=self.name,
                status=AgentStatus.COMPLETED,
                result=result,
                metadata={"investigation_id": context.investigation_id}
            )
            
        except Exception as e:
            # Record investigation failure
            INVESTIGATIONS_TOTAL.labels(
                agent_type="zumbi",
                investigation_type="general",  # Fallback for failed investigations
                status="failed"
            ).inc()
            
            self.logger.error(
                "investigation_failed",
                investigation_id=context.investigation_id,
                error=str(e),
                agent_name=self.name,
            )
            
            return AgentResponse(
                agent_name=self.name,
                status=AgentStatus.ERROR,
                error=str(e),
                result={
                    "status": "error",
                    "error": str(e),
                    "investigation_id": context.investigation_id,
                },
                metadata={"investigation_id": context.investigation_id}
            )
    
    async def _fetch_investigation_data(
        self,
        request: InvestigationRequest,
        context: AgentContext
    ) -> List[Dict[str, Any]]:
        """
        Fetch data from Portal da Transparência for investigation.
        
        Args:
            request: Investigation parameters
            context: Agent context
            
        Returns:
            List of contract records for analysis
        """
        all_contracts = []
        
        # Default organization codes if not specified
        org_codes = request.organization_codes or ["26000", "20000", "25000"]  # Health, Presidency, Education
        
        async with TransparencyAPIClient() as client:
            for org_code in org_codes:
                try:
                    # Create filters for this organization
                    filters = TransparencyAPIFilter(
                        codigo_orgao=org_code,
                        ano=2024,  # Current year
                        pagina=1,
                        tamanho_pagina=min(request.max_records // len(org_codes), 50)
                    )
                    
                    # Add date range if specified
                    if request.date_range:
                        filters.data_inicio = request.date_range[0]
                        filters.data_fim = request.date_range[1]
                    
                    # Add value threshold if specified
                    if request.value_threshold:
                        filters.valor_inicial = request.value_threshold
                    
                    # Fetch contracts
                    response = await client.get_contracts(filters)
                    
                    # Record API data fetched
                    TRANSPARENCY_API_DATA_FETCHED.labels(
                        endpoint="contracts",
                        organization=org_code,
                        status="success"
                    ).inc(len(response.data))
                    
                    # Add organization code to each contract
                    for contract in response.data:
                        contract["_org_code"] = org_code
                    
                    all_contracts.extend(response.data)
                    
                    self.logger.info(
                        "data_fetched",
                        org_code=org_code,
                        records=len(response.data),
                        investigation_id=context.investigation_id,
                    )
                    
                except Exception as e:
                    # Record API fetch failure
                    TRANSPARENCY_API_DATA_FETCHED.labels(
                        endpoint="contracts",
                        organization=org_code,
                        status="failed"
                    ).inc()
                    
                    self.logger.warning(
                        "data_fetch_failed",
                        org_code=org_code,
                        error=str(e),
                        investigation_id=context.investigation_id,
                    )
                    continue
        
        return all_contracts[:request.max_records]
    
    async def _enrich_with_open_data(
        self,
        contracts_data: List[Dict[str, Any]],
        context: AgentContext
    ) -> List[Dict[str, Any]]:
        """
        Enrich contract data with information from dados.gov.br.
        
        Args:
            contracts_data: Contract records
            context: Agent context
            
        Returns:
            Enriched contract data
        """
        # Extract unique organizations from contracts
        organizations = set()
        for contract in contracts_data:
            org_name = contract.get("orgao", {}).get("nome", "")
            if org_name:
                organizations.add(org_name)
        
        # Search for related datasets for each organization
        related_datasets = {}
        for org_name in organizations:
            try:
                # Search for datasets from this organization
                result = await self.dados_gov_tool._execute(
                    query=f"{org_name}, licitações, contratos",
                    action="search",
                    limit=5
                )
                
                if result.success and result.data:
                    related_datasets[org_name] = result.data.get("datasets", [])
                    
                    self.logger.info(
                        "open_data_found",
                        organization=org_name,
                        datasets_count=len(related_datasets[org_name]),
                        investigation_id=context.investigation_id,
                    )
            except Exception as e:
                self.logger.warning(
                    "open_data_search_failed",
                    organization=org_name,
                    error=str(e),
                    investigation_id=context.investigation_id,
                )
        
        # Enrich contracts with open data references
        for contract in contracts_data:
            org_name = contract.get("orgao", {}).get("nome", "")
            if org_name in related_datasets:
                contract["_open_data_available"] = True
                contract["_related_datasets"] = related_datasets[org_name]
            else:
                contract["_open_data_available"] = False
                contract["_related_datasets"] = []
        
        return contracts_data
    
    async def _run_anomaly_detection(
        self,
        contracts_data: List[Dict[str, Any]],
        request: InvestigationRequest,
        context: AgentContext
    ) -> List[AnomalyResult]:
        """
        Run all anomaly detection algorithms on the contract data.
        
        Args:
            contracts_data: Contract records to analyze
            request: Investigation parameters
            context: Agent context
            
        Returns:
            List of detected anomalies
        """
        all_anomalies = []
        
        # Determine which anomaly types to run
        types_to_run = request.anomaly_types or list(self.anomaly_detectors.keys())
        
        for anomaly_type in types_to_run:
            if anomaly_type in self.anomaly_detectors:
                try:
                    detector = self.anomaly_detectors[anomaly_type]
                    anomalies = await detector(contracts_data, context)
                    all_anomalies.extend(anomalies)
                    
                    self.logger.info(
                        "anomaly_detection_completed",
                        type=anomaly_type,
                        anomalies_found=len(anomalies),
                        investigation_id=context.investigation_id,
                    )
                    
                except Exception as e:
                    self.logger.error(
                        "anomaly_detection_failed",
                        type=anomaly_type,
                        error=str(e),
                        investigation_id=context.investigation_id,
                    )
        
        # Sort anomalies by severity (descending)
        all_anomalies.sort(key=lambda x: x.severity, reverse=True)
        
        return all_anomalies
    
    async def _detect_price_anomalies(
        self,
        contracts_data: List[Dict[str, Any]],
        context: AgentContext
    ) -> List[AnomalyResult]:
        """
        Detect contracts with anomalous pricing.
        
        Args:
            contracts_data: Contract records
            context: Agent context
            
        Returns:
            List of price anomalies
        """
        anomalies = []
        
        # Extract contract values
        values = []
        valid_contracts = []
        
        for contract in contracts_data:
            valor = contract.get("valorInicial") or contract.get("valorGlobal")
            if valor and isinstance(valor, (int, float)) and valor > 0:
                values.append(float(valor))
                valid_contracts.append(contract)
        
        if len(values) < 10:  # Need minimum samples for statistical analysis
            return anomalies
        
        # Calculate statistical measures
        values_array = np.array(values)
        mean_value = np.mean(values_array)
        std_value = np.std(values_array)
        
        # Detect outliers using z-score
        z_scores = np.abs((values_array - mean_value) / std_value)
        
        for i, (contract, value, z_score) in enumerate(zip(valid_contracts, values, z_scores)):
            if z_score > self.price_threshold:
                severity = min(z_score / 5.0, 1.0)  # Normalize to 0-1
                confidence = min(z_score / 3.0, 1.0)
                
                anomaly = AnomalyResult(
                    anomaly_type="price_anomaly",
                    severity=severity,
                    confidence=confidence,
                    description=f"Contrato com valor suspeito: R$ {value:,.2f}",
                    explanation=(
                        f"O valor deste contrato está {z_score:.1f} desvios padrão acima da média "
                        f"(R$ {mean_value:,.2f}). Valores muito acima do padrão podem indicar "
                        f"superfaturamento ou irregularidades no processo licitatório."
                    ),
                    evidence={
                        "contract_value": value,
                        "mean_value": mean_value,
                        "std_deviation": std_value,
                        "z_score": z_score,
                        "percentile": np.percentile(values_array, 95),
                    },
                    recommendations=[
                        "Investigar justificativas para o valor elevado",
                        "Comparar com contratos similares de outros órgãos",
                        "Verificar processo licitatório e documentação",
                        "Analisar histórico do fornecedor",
                    ],
                    affected_entities=[{
                        "contract_id": contract.get("id"),
                        "object": contract.get("objeto", "")[:100],
                        "supplier": contract.get("fornecedor", {}).get("nome", "N/A"),
                        "organization": contract.get("_org_code"),
                    }],
                    financial_impact=value - mean_value,
                )
                
                anomalies.append(anomaly)
        
        return anomalies
    
    async def _detect_vendor_concentration(
        self,
        contracts_data: List[Dict[str, Any]],
        context: AgentContext
    ) -> List[AnomalyResult]:
        """
        Detect excessive vendor concentration (potential monopolization).
        
        Args:
            contracts_data: Contract records
            context: Agent context
            
        Returns:
            List of vendor concentration anomalies
        """
        anomalies = []
        
        # Group contracts by vendor
        vendor_stats = {}
        total_value = 0
        
        for contract in contracts_data:
            supplier = contract.get("fornecedor", {})
            vendor_name = supplier.get("nome", "Unknown")
            vendor_cnpj = supplier.get("cnpj", "Unknown")
            vendor_key = f"{vendor_name}|{vendor_cnpj}"
            
            valor = contract.get("valorInicial") or contract.get("valorGlobal") or 0
            if isinstance(valor, (int, float)):
                valor = float(valor)
                total_value += valor
                
                if vendor_key not in vendor_stats:
                    vendor_stats[vendor_key] = {
                        "name": vendor_name,
                        "cnpj": vendor_cnpj,
                        "contracts": [],
                        "total_value": 0,
                        "contract_count": 0,
                    }
                
                vendor_stats[vendor_key]["contracts"].append(contract)
                vendor_stats[vendor_key]["total_value"] += valor
                vendor_stats[vendor_key]["contract_count"] += 1
        
        if total_value == 0:
            return anomalies
        
        # Check for concentration anomalies
        for vendor_key, stats in vendor_stats.items():
            concentration = stats["total_value"] / total_value
            
            if concentration > self.concentration_threshold:
                severity = min(concentration * 1.5, 1.0)
                confidence = concentration
                
                anomaly = AnomalyResult(
                    anomaly_type="vendor_concentration",
                    severity=severity,
                    confidence=confidence,
                    description=f"Concentração excessiva de contratos: {stats['name']}",
                    explanation=(
                        f"O fornecedor {stats['name']} concentra {concentration:.1%} do valor total "
                        f"dos contratos analisados ({stats['contract_count']} contratos). "
                        f"Alta concentração pode indicar direcionamento de licitações ou "
                        f"falta de competitividade no processo."
                    ),
                    evidence={
                        "vendor_name": stats["name"],
                        "vendor_cnpj": stats["cnpj"],
                        "concentration_percentage": concentration * 100,
                        "total_value": stats["total_value"],
                        "contract_count": stats["contract_count"],
                        "market_share": concentration,
                    },
                    recommendations=[
                        "Verificar se houve direcionamento nas licitações",
                        "Analisar competitividade do mercado",
                        "Investigar relacionamento entre órgão e fornecedor",
                        "Revisar critérios de seleção de fornecedores",
                    ],
                    affected_entities=[{
                        "vendor_name": stats["name"],
                        "vendor_cnpj": stats["cnpj"],
                        "contract_count": stats["contract_count"],
                        "total_value": stats["total_value"],
                    }],
                    financial_impact=stats["total_value"],
                )
                
                anomalies.append(anomaly)
        
        return anomalies
    
    async def _detect_temporal_anomalies(
        self,
        contracts_data: List[Dict[str, Any]],
        context: AgentContext
    ) -> List[AnomalyResult]:
        """
        Detect suspicious temporal patterns in contracts.
        
        Args:
            contracts_data: Contract records
            context: Agent context
            
        Returns:
            List of temporal anomalies
        """
        anomalies = []
        
        # Group contracts by date
        date_stats = {}
        
        for contract in contracts_data:
            # Try to extract date from different fields
            date_str = (
                contract.get("dataAssinatura") or 
                contract.get("dataPublicacao") or
                contract.get("dataInicio")
            )
            
            if date_str:
                try:
                    # Parse date (assuming DD/MM/YYYY format)
                    date_parts = date_str.split("/")
                    if len(date_parts) == 3:
                        day = int(date_parts[0])
                        month = int(date_parts[1])
                        year = int(date_parts[2])
                        
                        date_key = f"{year}-{month:02d}"
                        
                        if date_key not in date_stats:
                            date_stats[date_key] = {
                                "contracts": [],
                                "count": 0,
                                "total_value": 0,
                            }
                        
                        valor = contract.get("valorInicial") or contract.get("valorGlobal") or 0
                        if isinstance(valor, (int, float)):
                            date_stats[date_key]["total_value"] += float(valor)
                        
                        date_stats[date_key]["contracts"].append(contract)
                        date_stats[date_key]["count"] += 1
                        
                except (ValueError, IndexError):
                    continue
        
        if len(date_stats) < 3:  # Need minimum periods for comparison
            return anomalies
        
        # Calculate average contracts per period
        counts = [stats["count"] for stats in date_stats.values()]
        mean_count = np.mean(counts)
        std_count = np.std(counts)
        
        # Look for periods with unusually high activity
        for date_key, stats in date_stats.items():
            if std_count > 0:
                z_score = (stats["count"] - mean_count) / std_count
                
                if z_score > 2.0:  # More than 2 standard deviations
                    severity = min(z_score / 4.0, 1.0)
                    confidence = min(z_score / 3.0, 1.0)
                    
                    anomaly = AnomalyResult(
                        anomaly_type="temporal_patterns",
                        severity=severity,
                        confidence=confidence,
                        description=f"Atividade contratual suspeita em {date_key}",
                        explanation=(
                            f"Em {date_key} foram assinados {stats['count']} contratos, "
                            f"{z_score:.1f} desvios padrão acima da média ({mean_count:.1f}). "
                            f"Picos de atividade podem indicar direcionamento ou urgência "
                            f"inadequada nos processos."
                        ),
                        evidence={
                            "period": date_key,
                            "contract_count": stats["count"],
                            "mean_count": mean_count,
                            "z_score": z_score,
                            "total_value": stats["total_value"],
                        },
                        recommendations=[
                            "Investigar justificativas para a concentração temporal",
                            "Verificar se houve emergência ou urgência",
                            "Analisar qualidade dos processos licitatórios",
                            "Revisar planejamento de contratações",
                        ],
                        affected_entities=[{
                            "period": date_key,
                            "contract_count": stats["count"],
                            "total_value": stats["total_value"],
                        }],
                        financial_impact=stats["total_value"],
                    )
                    
                    anomalies.append(anomaly)
        
        return anomalies
    
    async def _detect_duplicate_contracts(
        self,
        contracts_data: List[Dict[str, Any]],
        context: AgentContext
    ) -> List[AnomalyResult]:
        """
        Detect potentially duplicate or very similar contracts.
        
        Args:
            contracts_data: Contract records
            context: Agent context
            
        Returns:
            List of duplicate contract anomalies
        """
        anomalies = []
        
        # Simple similarity detection based on object description
        for i, contract1 in enumerate(contracts_data):
            objeto1 = contract1.get("objeto", "").lower()
            if len(objeto1) < 20:  # Skip very short descriptions
                continue
                
            for j, contract2 in enumerate(contracts_data[i+1:], start=i+1):
                objeto2 = contract2.get("objeto", "").lower()
                if len(objeto2) < 20:
                    continue
                
                # Calculate simple similarity (Jaccard similarity of words)
                words1 = set(objeto1.split())
                words2 = set(objeto2.split())
                
                if len(words1) == 0 or len(words2) == 0:
                    continue
                
                intersection = len(words1.intersection(words2))
                union = len(words1.union(words2))
                similarity = intersection / union if union > 0 else 0
                
                if similarity > self.duplicate_threshold:
                    severity = similarity
                    confidence = similarity
                    
                    valor1 = contract1.get("valorInicial") or contract1.get("valorGlobal") or 0
                    valor2 = contract2.get("valorInicial") or contract2.get("valorGlobal") or 0
                    
                    anomaly = AnomalyResult(
                        anomaly_type="duplicate_contracts",
                        severity=severity,
                        confidence=confidence,
                        description="Contratos potencialmente duplicados detectados",
                        explanation=(
                            f"Dois contratos com {similarity:.1%} de similaridade foram "
                            f"encontrados. Contratos similares podem indicar pagamentos "
                            f"duplicados ou direcionamento inadequado."
                        ),
                        evidence={
                            "similarity_score": similarity,
                            "contract1_id": contract1.get("id"),
                            "contract2_id": contract2.get("id"),
                            "contract1_value": valor1,
                            "contract2_value": valor2,
                            "object1": objeto1[:100],
                            "object2": objeto2[:100],
                        },
                        recommendations=[
                            "Verificar se são contratos distintos ou duplicados",
                            "Analisar justificativas para objetos similares",
                            "Investigar fornecedores envolvidos",
                            "Revisar controles internos de contratação",
                        ],
                        affected_entities=[
                            {
                                "contract_id": contract1.get("id"),
                                "object": objeto1[:100],
                                "value": valor1,
                            },
                            {
                                "contract_id": contract2.get("id"),
                                "object": objeto2[:100],
                                "value": valor2,
                            },
                        ],
                        financial_impact=float(valor1) + float(valor2) if isinstance(valor1, (int, float)) and isinstance(valor2, (int, float)) else None,
                    )
                    
                    anomalies.append(anomaly)
        
        return anomalies
    
    async def _detect_payment_anomalies(
        self,
        contracts_data: List[Dict[str, Any]],
        context: AgentContext
    ) -> List[AnomalyResult]:
        """
        Detect unusual payment patterns in contracts.
        
        Args:
            contracts_data: Contract records
            context: Agent context
            
        Returns:
            List of payment anomalies
        """
        anomalies = []
        
        # Look for contracts with unusual value patterns
        for contract in contracts_data:
            valor_inicial = contract.get("valorInicial")
            valor_global = contract.get("valorGlobal")
            
            if valor_inicial and valor_global:
                try:
                    inicial = float(valor_inicial)
                    global_val = float(valor_global)
                    
                    # Check for significant discrepancies
                    if inicial > 0 and global_val > 0:
                        ratio = abs(inicial - global_val) / max(inicial, global_val)
                        
                        if ratio > 0.5:  # 50% discrepancy threshold
                            severity = min(ratio, 1.0)
                            confidence = ratio
                            
                            anomaly = AnomalyResult(
                                anomaly_type="payment_patterns",
                                severity=severity,
                                confidence=confidence,
                                description="Discrepância significativa entre valores do contrato",
                                explanation=(
                                    f"Diferença de {ratio:.1%} entre valor inicial "
                                    f"(R$ {inicial:,.2f}) e valor global (R$ {global_val:,.2f}). "
                                    f"Grandes discrepâncias podem indicar aditivos excessivos "
                                    f"ou irregularidades nos pagamentos."
                                ),
                                evidence={
                                    "valor_inicial": inicial,
                                    "valor_global": global_val,
                                    "discrepancy_ratio": ratio,
                                    "absolute_difference": abs(inicial - global_val),
                                },
                                recommendations=[
                                    "Investigar justificativas para alterações de valor",
                                    "Verificar aditivos contratuais",
                                    "Analisar execução e pagamentos realizados",
                                    "Revisar controles de alteração contratual",
                                ],
                                affected_entities=[{
                                    "contract_id": contract.get("id"),
                                    "object": contract.get("objeto", "")[:100],
                                    "supplier": contract.get("fornecedor", {}).get("nome", "N/A"),
                                }],
                                financial_impact=abs(inicial - global_val),
                            )
                            
                            anomalies.append(anomaly)
                            
                except (ValueError, TypeError):
                    continue
        
        return anomalies
    
    async def _detect_spectral_anomalies(
        self,
        contracts_data: List[Dict[str, Any]],
        context: AgentContext
    ) -> List[AnomalyResult]:
        """
        Detect anomalies using spectral analysis and Fourier transforms.
        
        Args:
            contracts_data: Contract records
            context: Agent context
            
        Returns:
            List of spectral anomalies
        """
        anomalies = []
        
        try:
            # Prepare time series data
            time_series_data = self._prepare_time_series(contracts_data)
            
            if len(time_series_data) < 30:  # Need sufficient data points
                self.logger.warning("insufficient_data_for_spectral_analysis", data_points=len(time_series_data))
                return anomalies
            
            # Extract spending values and timestamps
            spending_data = pd.Series([item['value'] for item in time_series_data])
            timestamps = pd.DatetimeIndex([item['date'] for item in time_series_data])
            
            # Perform spectral anomaly detection
            spectral_anomalies = self.spectral_analyzer.detect_anomalies(
                spending_data, 
                timestamps,
                context={'entity_name': context.investigation_id if hasattr(context, 'investigation_id') else 'Unknown'}
            )
            
            # Convert SpectralAnomaly objects to AnomalyResult objects
            for spec_anomaly in spectral_anomalies:
                anomaly = AnomalyResult(
                    anomaly_type=f"spectral_{spec_anomaly.anomaly_type}",
                    severity=spec_anomaly.anomaly_score,
                    confidence=spec_anomaly.anomaly_score,
                    description=spec_anomaly.description,
                    explanation=self._create_spectral_explanation(spec_anomaly),
                    evidence={
                        "frequency_band": spec_anomaly.frequency_band,
                        "anomaly_score": spec_anomaly.anomaly_score,
                        "timestamp": spec_anomaly.timestamp.isoformat(),
                        **spec_anomaly.evidence
                    },
                    recommendations=spec_anomaly.recommendations,
                    affected_entities=self._extract_affected_entities_from_spectral(spec_anomaly, contracts_data),
                    financial_impact=self._calculate_spectral_financial_impact(spec_anomaly, spending_data)
                )
                anomalies.append(anomaly)
            
            # Find periodic patterns
            periodic_patterns = self.spectral_analyzer.find_periodic_patterns(
                spending_data,
                timestamps,
                entity_name=context.investigation_id if hasattr(context, 'investigation_id') else None
            )
            
            # Convert suspicious periodic patterns to anomalies
            for pattern in periodic_patterns:
                if pattern.pattern_type == "suspicious" or pattern.amplitude > 0.5:
                    anomaly = AnomalyResult(
                        anomaly_type="suspicious_periodic_pattern",
                        severity=pattern.amplitude,
                        confidence=pattern.confidence,
                        description=f"Padrão periódico suspeito detectado (período: {pattern.period_days:.1f} dias)",
                        explanation=(
                            f"Detectado padrão de gastos com periodicidade de {pattern.period_days:.1f} dias "
                            f"e amplitude de {pattern.amplitude:.1%}. {pattern.business_interpretation}"
                        ),
                        evidence={
                            "period_days": pattern.period_days,
                            "frequency_hz": pattern.frequency_hz,
                            "amplitude": pattern.amplitude,
                            "confidence": pattern.confidence,
                            "pattern_type": pattern.pattern_type,
                            "statistical_significance": pattern.statistical_significance
                        },
                        recommendations=[
                            "Investigar causa do padrão periódico",
                            "Verificar se há processos automatizados",
                            "Analisar justificativas para regularidade excessiva",
                            "Revisar cronograma de pagamentos"
                        ],
                        affected_entities=[{
                            "pattern_type": pattern.pattern_type,
                            "period_days": pattern.period_days,
                            "amplitude": pattern.amplitude
                        }],
                        financial_impact=float(spending_data.sum() * pattern.amplitude)
                    )
                    anomalies.append(anomaly)
            
            self.logger.info(
                "spectral_analysis_completed",
                spectral_anomalies_count=len(spectral_anomalies),
                periodic_patterns_count=len(periodic_patterns),
                total_anomalies=len(anomalies)
            )
            
        except Exception as e:
            self.logger.error(f"Error in spectral anomaly detection: {str(e)}")
            # Don't fail the entire investigation if spectral analysis fails
        
        return anomalies
    
    def _prepare_time_series(self, contracts_data: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
        """Prepare time series data from contracts for spectral analysis."""
        time_series = []
        
        for contract in contracts_data:
            # Extract date
            date_str = (
                contract.get("dataAssinatura") or 
                contract.get("dataPublicacao") or
                contract.get("dataInicio")
            )
            
            if not date_str:
                continue
                
            try:
                # Parse date (DD/MM/YYYY format)
                date_parts = date_str.split("/")
                if len(date_parts) == 3:
                    day, month, year = int(date_parts[0]), int(date_parts[1]), int(date_parts[2])
                    date_obj = datetime(year, month, day)
                    
                    # Extract value
                    valor = contract.get("valorInicial") or contract.get("valorGlobal") or 0
                    if isinstance(valor, (int, float)) and valor > 0:
                        time_series.append({
                            'date': date_obj,
                            'value': float(valor),
                            'contract_id': contract.get('id'),
                            'supplier': contract.get('fornecedor', {}).get('nome', 'N/A')
                        })
                        
            except (ValueError, IndexError):
                continue
        
        # Sort by date
        time_series.sort(key=lambda x: x['date'])
        
        # Aggregate by date (sum values for same dates)
        daily_aggregates = {}
        for item in time_series:
            date_key = item['date'].date()
            if date_key not in daily_aggregates:
                daily_aggregates[date_key] = {
                    'date': datetime.combine(date_key, datetime.min.time()),
                    'value': 0,
                    'contract_count': 0,
                    'suppliers': set()
                }
            daily_aggregates[date_key]['value'] += item['value']
            daily_aggregates[date_key]['contract_count'] += 1
            daily_aggregates[date_key]['suppliers'].add(item['supplier'])
        
        # Convert back to list
        aggregated_series = []
        for date_key in sorted(daily_aggregates.keys()):
            data = daily_aggregates[date_key]
            aggregated_series.append({
                'date': data['date'],
                'value': data['value'],
                'contract_count': data['contract_count'],
                'unique_suppliers': len(data['suppliers'])
            })
        
        return aggregated_series
    
    def _create_spectral_explanation(self, spec_anomaly: SpectralAnomaly) -> str:
        """Create detailed explanation for spectral anomaly."""
        explanations = {
            "high_frequency_pattern": (
                "Detectado padrão de alta frequência nos gastos públicos. "
                "Padrões muito regulares podem indicar manipulação sistemática ou "
                "processos automatizados não documentados."
            ),
            "spectral_regime_change": (
                "Mudança significativa detectada na complexidade dos padrões de gastos. "
                "Alterações bruscas podem indicar mudanças de política, procedimentos "
                "ou possível manipulação."
            ),
            "excessive_quarterly_pattern": (
                "Padrão excessivo de gastos trimestrais detectado. "
                "Concentração de gastos no final de trimestres pode indicar "
                "execução inadequada de orçamento ou 'correria' para gastar verbas."
            ),
            "unusual_weekly_regularity": (
                "Regularidade semanal incomum detectada nos gastos. "
                "Padrões muito regulares em gastos governamentais podem ser suspeitos "
                "se não corresponderem a processos de negócio conhecidos."
            ),
            "high_frequency_noise": (
                "Ruído de alta frequência detectado nos dados de gastos. "
                "Pode indicar problemas na coleta de dados ou manipulação artificial "
                "dos valores reportados."
            )
        }
        
        base_explanation = explanations.get(
            spec_anomaly.anomaly_type,
            f"Anomalia espectral detectada: {spec_anomaly.description}"
        )
        
        return f"{base_explanation} Score de anomalia: {spec_anomaly.anomaly_score:.2f}. {spec_anomaly.description}"
    
    def _extract_affected_entities_from_spectral(
        self, 
        spec_anomaly: SpectralAnomaly, 
        contracts_data: List[Dict[str, Any]]
    ) -> List[Dict[str, Any]]:
        """Extract affected entities from spectral anomaly context."""
        affected = []
        
        # For temporal anomalies, find contracts around the anomaly timestamp
        if hasattr(spec_anomaly, 'timestamp') and spec_anomaly.timestamp:
            anomaly_date = spec_anomaly.timestamp.date()
            
            for contract in contracts_data:
                date_str = (
                    contract.get("dataAssinatura") or 
                    contract.get("dataPublicacao") or
                    contract.get("dataInicio")
                )
                
                if date_str:
                    try:
                        date_parts = date_str.split("/")
                        if len(date_parts) == 3:
                            day, month, year = int(date_parts[0]), int(date_parts[1]), int(date_parts[2])
                            contract_date = datetime(year, month, day).date()
                            
                            # Include contracts within a week of the anomaly
                            if abs((contract_date - anomaly_date).days) <= 7:
                                affected.append({
                                    "contract_id": contract.get("id"),
                                    "date": date_str,
                                    "supplier": contract.get("fornecedor", {}).get("nome", "N/A"),
                                    "value": contract.get("valorInicial") or contract.get("valorGlobal") or 0,
                                    "object": contract.get("objeto", "")[:100]
                                })
                    except (ValueError, IndexError):
                        continue
        
        return affected[:10]  # Limit to first 10 to avoid overwhelming
    
    def _calculate_spectral_financial_impact(
        self, 
        spec_anomaly: SpectralAnomaly, 
        spending_data: pd.Series
    ) -> Optional[float]:
        """Calculate financial impact of spectral anomaly."""
        try:
            # For high-amplitude anomalies, estimate impact as percentage of total spending
            if hasattr(spec_anomaly, 'anomaly_score') and spec_anomaly.anomaly_score > 0:
                total_spending = float(spending_data.sum())
                impact_ratio = min(spec_anomaly.anomaly_score, 0.5)  # Cap at 50%
                return total_spending * impact_ratio
        except:
            pass
        
        return None
    
    def _generate_investigation_summary(
        self,
        contracts_data: List[Dict[str, Any]],
        anomalies: List[AnomalyResult]
    ) -> Dict[str, Any]:
        """Generate summary statistics for the investigation."""
        total_value = 0
        suspicious_value = 0
        
        # Calculate total contract value
        for contract in contracts_data:
            valor = contract.get("valorInicial") or contract.get("valorGlobal") or 0
            if isinstance(valor, (int, float)):
                total_value += float(valor)
        
        # Calculate suspicious value
        for anomaly in anomalies:
            if anomaly.financial_impact:
                suspicious_value += anomaly.financial_impact
        
        # Group anomalies by type
        anomaly_counts = {}
        for anomaly in anomalies:
            anomaly_type = anomaly.anomaly_type
            anomaly_counts[anomaly_type] = anomaly_counts.get(anomaly_type, 0) + 1
        
        # Calculate risk score
        risk_score = min(len(anomalies) / max(len(contracts_data), 1) * 10, 10)
        
        return {
            "total_records": len(contracts_data),
            "anomalies_found": len(anomalies),
            "total_value": total_value,
            "suspicious_value": suspicious_value,
            "risk_score": risk_score,
            "anomaly_types": anomaly_counts,
            "high_severity_count": len([a for a in anomalies if a.severity > 0.7]),
            "medium_severity_count": len([a for a in anomalies if 0.3 < a.severity <= 0.7]),
            "low_severity_count": len([a for a in anomalies if a.severity <= 0.3]),
        }
    
    def _anomaly_to_dict(self, anomaly: AnomalyResult) -> Dict[str, Any]:
        """Convert AnomalyResult to dictionary for serialization."""
        return {
            "type": anomaly.anomaly_type,
            "severity": anomaly.severity,
            "confidence": anomaly.confidence,
            "description": anomaly.description,
            "explanation": anomaly.explanation,
            "evidence": anomaly.evidence,
            "recommendations": anomaly.recommendations,
            "affected_entities": anomaly.affected_entities,
            "financial_impact": anomaly.financial_impact,
        }