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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
"""
Investigation service with Supabase backend integration.

This version replaces the in-memory storage with Supabase PostgreSQL,
providing persistent storage shared with the frontend.
"""

from typing import List, Optional, Dict, Any
from datetime import datetime
import uuid

from src.core import get_logger
from src.services.supabase_service import get_supabase_service
from src.agents import MasterAgent, get_agent_pool
from src.agents.deodoro import AgentContext

logger = get_logger(__name__)


class InvestigationServiceSupabase:
    """
    Service for managing investigations with Supabase persistence.

    This service provides a bridge between the agent system and Supabase,
    allowing the frontend to access investigation results in real-time.
    """

    def __init__(self):
        """Initialize investigation service."""
        self._supabase = None

    async def _get_supabase(self):
        """Lazy load Supabase service."""
        if self._supabase is None:
            self._supabase = await get_supabase_service()
        return self._supabase

    async def create(
        self,
        user_id: str,
        query: str,
        data_source: str = "contracts",
        filters: Optional[Dict[str, Any]] = None,
        anomaly_types: Optional[List[str]] = None,
        session_id: Optional[str] = None,
    ) -> Dict[str, Any]:
        """
        Create a new investigation in Supabase.

        Args:
            user_id: User ID
            query: Investigation query
            data_source: Data source to investigate
            filters: Query filters
            anomaly_types: Types of anomalies to detect
            session_id: Optional session ID

        Returns:
            Created investigation dict
        """
        supabase = await self._get_supabase()

        investigation = await supabase.create_investigation(
            user_id=user_id,
            query=query,
            data_source=data_source,
            filters=filters or {},
            anomaly_types=anomaly_types or [],
            session_id=session_id,
        )

        logger.info(
            "investigation_created",
            investigation_id=investigation["id"],
            user_id=user_id,
            data_source=data_source,
        )

        return investigation

    async def start_investigation(
        self,
        investigation_id: str,
    ) -> None:
        """
        Start processing an investigation in the background.

        Args:
            investigation_id: Investigation UUID
        """
        supabase = await self._get_supabase()

        # Get investigation details
        investigation = await supabase.get_investigation(investigation_id)

        if not investigation:
            raise ValueError(f"Investigation {investigation_id} not found")

        # Update to processing status
        await supabase.update_investigation(
            investigation_id,
            status="processing",
            started_at=datetime.utcnow(),
            progress=0.1,
            current_phase="initializing",
        )

        try:
            # Execute investigation with agents
            await self._execute_investigation(investigation)

        except Exception as e:
            logger.error(
                "investigation_execution_failed",
                investigation_id=investigation_id,
                error=str(e),
                exc_info=True,
            )

            # Mark as failed in Supabase
            await supabase.fail_investigation(
                investigation_id,
                error_message=str(e),
            )
            raise

    async def _execute_investigation(self, investigation: Dict[str, Any]):
        """
        Execute investigation using the agent system.

        Args:
            investigation: Investigation dict from database
        """
        investigation_id = investigation["id"]
        supabase = await self._get_supabase()

        # Update progress: data retrieval
        await supabase.update_progress(
            investigation_id,
            progress=0.2,
            current_phase="data_retrieval",
        )

        # Get agent pool
        pool = await get_agent_pool()

        # Create agent context
        context = AgentContext(
            investigation_id=investigation_id,
            user_id=investigation["user_id"],
            session_id=investigation.get("session_id"),
            metadata={
                "data_source": investigation["data_source"],
                "filters": investigation.get("filters", {}),
                "anomaly_types": investigation.get("anomaly_types", []),
            }
        )

        # Update progress: anomaly detection
        await supabase.update_progress(
            investigation_id,
            progress=0.4,
            current_phase="anomaly_detection",
        )

        # Execute with master agent (or directly with investigator)
        # For now, we'll simulate the investigation
        # TODO: Replace with actual agent execution

        from src.agents import InvestigatorAgent
        investigator = InvestigatorAgent()

        # Prepare investigation parameters
        from src.tools import TransparencyAPIFilter
        filters = TransparencyAPIFilter(**investigation.get("filters", {}))

        # Execute investigation
        results = await investigator.investigate_anomalies(
            query=investigation["query"],
            data_source=investigation["data_source"],
            filters=filters,
            anomaly_types=investigation.get("anomaly_types", []),
            context=context,
        )

        # Update progress: analysis
        await supabase.update_progress(
            investigation_id,
            progress=0.7,
            current_phase="analysis",
            records_processed=sum(len(r.affected_data) for r in results),
            anomalies_found=len(results),
        )

        # Generate summary
        summary = await investigator.generate_summary(results, context)

        # Calculate confidence
        confidence_score = (
            sum(r.confidence for r in results) / len(results)
            if results else 0.0
        )

        # Format results for storage
        formatted_results = [
            {
                "anomaly_id": str(uuid.uuid4()),
                "type": result.anomaly_type,
                "severity": result.severity,
                "confidence": result.confidence,
                "description": result.description,
                "explanation": result.explanation,
                "affected_records": result.affected_data,
                "suggested_actions": result.recommendations,
                "metadata": result.metadata,
            }
            for result in results
        ]

        # Complete investigation in Supabase
        await supabase.complete_investigation(
            investigation_id=investigation_id,
            results=formatted_results,
            summary=summary,
            confidence_score=confidence_score,
            total_records=sum(len(r.affected_data) for r in results),
            anomalies_found=len(results),
        )

        logger.info(
            "investigation_completed",
            investigation_id=investigation_id,
            anomalies_found=len(results),
            confidence_score=confidence_score,
        )

    async def update_progress(
        self,
        investigation_id: str,
        progress: float,
        current_phase: str,
        records_processed: Optional[int] = None,
        anomalies_found: Optional[int] = None,
    ) -> Dict[str, Any]:
        """
        Update investigation progress.

        Args:
            investigation_id: Investigation UUID
            progress: Progress percentage (0.0 to 1.0)
            current_phase: Current processing phase
            records_processed: Number of records processed
            anomalies_found: Number of anomalies detected

        Returns:
            Updated investigation dict
        """
        supabase = await self._get_supabase()
        return await supabase.update_progress(
            investigation_id=investigation_id,
            progress=progress,
            current_phase=current_phase,
            records_processed=records_processed,
            anomalies_found=anomalies_found,
        )

    async def complete_investigation(
        self,
        investigation_id: str,
        results: List[Dict[str, Any]],
        summary: str,
        confidence_score: float,
        total_records: int = 0,
        anomalies_found: int = 0,
    ) -> Dict[str, Any]:
        """
        Mark investigation as completed with results.

        Args:
            investigation_id: Investigation UUID
            results: List of anomaly results
            summary: Investigation summary
            confidence_score: Overall confidence
            total_records: Total records analyzed
            anomalies_found: Total anomalies found

        Returns:
            Updated investigation dict
        """
        supabase = await self._get_supabase()
        return await supabase.complete_investigation(
            investigation_id=investigation_id,
            results=results,
            summary=summary,
            confidence_score=confidence_score,
            total_records=total_records,
            anomalies_found=anomalies_found,
        )

    async def get(self, investigation_id: str) -> Optional[Dict[str, Any]]:
        """
        Get investigation by ID (alias for get_by_id).

        Args:
            investigation_id: Investigation UUID

        Returns:
            Investigation dict or None
        """
        return await self.get_by_id(investigation_id)

    async def get_by_id(self, investigation_id: str) -> Optional[Dict[str, Any]]:
        """
        Get investigation by ID.

        Args:
            investigation_id: Investigation UUID

        Returns:
            Investigation dict or None
        """
        supabase = await self._get_supabase()
        return await supabase.get_investigation(investigation_id)

    async def update_status(
        self,
        investigation_id: str,
        status: str,
        progress: Optional[float] = None,
        current_phase: Optional[str] = None,
        **kwargs
    ) -> Dict[str, Any]:
        """
        Update investigation status and progress.

        Args:
            investigation_id: Investigation UUID
            status: New status
            progress: Progress percentage
            current_phase: Current phase
            **kwargs: Additional fields to update

        Returns:
            Updated investigation dict
        """
        supabase = await self._get_supabase()

        updates = {"status": status}

        if progress is not None:
            updates["progress"] = progress

        if current_phase is not None:
            updates["current_phase"] = current_phase

        updates.update(kwargs)

        return await supabase.update_investigation(investigation_id, **updates)

    async def search(
        self,
        user_id: Optional[str] = None,
        status: Optional[str] = None,
        limit: int = 20,
        offset: int = 0,
    ) -> List[Dict[str, Any]]:
        """
        Search investigations with filters.

        Args:
            user_id: Filter by user ID
            status: Filter by status
            limit: Maximum results
            offset: Pagination offset

        Returns:
            List of investigation dicts
        """
        if not user_id:
            raise ValueError("user_id is required for investigation search")

        supabase = await self._get_supabase()
        return await supabase.list_user_investigations(
            user_id=user_id,
            limit=limit,
            offset=offset,
            status=status,
        )

    async def cancel(self, investigation_id: str, user_id: str) -> Dict[str, Any]:
        """
        Cancel a running investigation.

        Args:
            investigation_id: Investigation UUID
            user_id: User ID (for authorization)

        Returns:
            Updated investigation dict
        """
        supabase = await self._get_supabase()

        # Get investigation to check ownership
        investigation = await supabase.get_investigation(investigation_id)

        if not investigation:
            raise ValueError(f"Investigation {investigation_id} not found")

        if investigation["user_id"] != user_id:
            raise ValueError("Unauthorized: investigation belongs to another user")

        if investigation["status"] in ["completed", "failed", "cancelled"]:
            raise ValueError(
                f"Cannot cancel investigation in {investigation['status']} status"
            )

        # Mark as cancelled
        deleted = await supabase.delete_investigation(investigation_id, user_id)

        if not deleted:
            raise ValueError(f"Failed to cancel investigation {investigation_id}")

        logger.info(
            "investigation_cancelled",
            investigation_id=investigation_id,
            user_id=user_id,
        )

        # Return updated investigation
        return await supabase.get_investigation(investigation_id)

    async def get_user_investigations(
        self,
        user_id: str,
        limit: int = 10
    ) -> List[Dict[str, Any]]:
        """
        Get investigations for a user.

        Args:
            user_id: User ID
            limit: Maximum results

        Returns:
            List of investigation dicts
        """
        return await self.search(user_id=user_id, limit=limit)


# Global service instance
investigation_service_supabase = InvestigationServiceSupabase()