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
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
"""
Module: infrastructure.queue.tasks.report_tasks
Description: Celery tasks for report generation and processing
Author: Anderson H. Silva
Date: 2025-01-25
License: Proprietary - All rights reserved
"""

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

from celery import chain, group
from celery.utils.log import get_task_logger

from src.infrastructure.queue.celery_app import celery_app, priority_task, TaskPriority
from src.services.report_service import ReportService
from src.services.export_service import ExportService
from src.core.dependencies import get_db_session
from src.agents import get_agent_pool

logger = get_task_logger(__name__)


@celery_app.task(name="tasks.generate_report", bind=True, queue="normal")
def generate_report(
    self,
    report_id: str,
    report_type: str,
    investigation_ids: List[str],
    config: Optional[Dict[str, Any]] = None
) -> Dict[str, Any]:
    """
    Generate a comprehensive report.
    
    Args:
        report_id: Unique report ID
        report_type: Type of report to generate
        investigation_ids: List of investigation IDs to include
        config: Report configuration
        
    Returns:
        Generated report data
    """
    try:
        logger.info(
            "report_generation_started",
            report_id=report_id,
            report_type=report_type,
            investigations=len(investigation_ids)
        )
        
        # Update task state
        self.update_state(
            state="PROGRESS",
            meta={
                "current": 0,
                "total": 100,
                "status": "Initializing report generation..."
            }
        )
        
        # Run async report generation
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        
        try:
            result = loop.run_until_complete(
                _generate_report_async(
                    self,
                    report_id,
                    report_type,
                    investigation_ids,
                    config
                )
            )
            
            logger.info(
                "report_generation_completed",
                report_id=report_id,
                word_count=result.get("word_count", 0)
            )
            
            return result
            
        finally:
            loop.close()
    
    except Exception as e:
        logger.error(
            "report_generation_failed",
            report_id=report_id,
            error=str(e),
            exc_info=True
        )
        
        # Retry with exponential backoff
        raise self.retry(
            exc=e,
            countdown=60 * (2 ** self.request.retries),
            max_retries=3
        )


async def _generate_report_async(
    task,
    report_id: str,
    report_type: str,
    investigation_ids: List[str],
    config: Optional[Dict[str, Any]]
) -> Dict[str, Any]:
    """Async report generation implementation."""
    async with get_db_session() as db:
        report_service = ReportService(db)
        agent_pool = get_agent_pool()
        
        # Get Tiradentes agent for report generation
        tiradentes = agent_pool.get_agent("tiradentes")
        if not tiradentes:
            raise RuntimeError("Report generation agent not available")
        
        # Update progress
        task.update_state(
            state="PROGRESS",
            meta={
                "current": 20,
                "total": 100,
                "status": "Loading investigation data..."
            }
        )
        
        # Load investigations
        investigations = await report_service.load_investigations(investigation_ids)
        
        # Update progress
        task.update_state(
            state="PROGRESS",
            meta={
                "current": 40,
                "total": 100,
                "status": "Analyzing findings..."
            }
        )
        
        # Generate report content
        report_content = await tiradentes.generate_report(
            report_type=report_type,
            investigations=investigations,
            config=config or {}
        )
        
        # Update progress
        task.update_state(
            state="PROGRESS",
            meta={
                "current": 80,
                "total": 100,
                "status": "Finalizing report..."
            }
        )
        
        # Save report
        report = await report_service.save_report(
            report_id=report_id,
            report_type=report_type,
            content=report_content,
            metadata={
                "investigation_ids": investigation_ids,
                "generated_by": "tiradentes",
                "config": config
            }
        )
        
        # Update progress
        task.update_state(
            state="PROGRESS",
            meta={
                "current": 100,
                "total": 100,
                "status": "Report completed!"
            }
        )
        
        return {
            "report_id": report.id,
            "report_type": report_type,
            "title": report.title,
            "word_count": len(report_content.split()),
            "status": "completed",
            "created_at": report.created_at.isoformat()
        }


@celery_app.task(name="tasks.generate_executive_summary", queue="high")
def generate_executive_summary(
    investigation_ids: List[str],
    max_length: int = 500
) -> Dict[str, Any]:
    """
    Generate executive summary from investigations.
    
    Args:
        investigation_ids: Investigation IDs to summarize
        max_length: Maximum summary length in words
        
    Returns:
        Executive summary
    """
    logger.info(
        "executive_summary_started",
        investigations=len(investigation_ids),
        max_length=max_length
    )
    
    try:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        
        try:
            result = loop.run_until_complete(
                _generate_executive_summary_async(
                    investigation_ids,
                    max_length
                )
            )
            
            return result
            
        finally:
            loop.close()
            
    except Exception as e:
        logger.error(
            "executive_summary_failed",
            error=str(e),
            exc_info=True
        )
        raise


async def _generate_executive_summary_async(
    investigation_ids: List[str],
    max_length: int
) -> Dict[str, Any]:
    """Async executive summary generation."""
    async with get_db_session() as db:
        report_service = ReportService(db)
        agent_pool = get_agent_pool()
        
        # Get Tiradentes agent
        tiradentes = agent_pool.get_agent("tiradentes")
        if not tiradentes:
            raise RuntimeError("Report agent not available")
        
        # Load investigations
        investigations = await report_service.load_investigations(investigation_ids)
        
        # Generate summary
        summary = await tiradentes.generate_executive_summary(
            investigations=investigations,
            max_length=max_length
        )
        
        return {
            "summary": summary,
            "word_count": len(summary.split()),
            "investigation_count": len(investigations),
            "key_findings": await tiradentes.extract_key_findings(investigations),
            "generated_at": datetime.now().isoformat()
        }


@celery_app.task(name="tasks.batch_report_generation", queue="normal")
def batch_report_generation(
    report_configs: List[Dict[str, Any]]
) -> Dict[str, Any]:
    """
    Generate multiple reports in batch.
    
    Args:
        report_configs: List of report configurations
        
    Returns:
        Batch generation results
    """
    logger.info(
        "batch_report_generation_started",
        report_count=len(report_configs)
    )
    
    # Create subtasks for each report
    tasks = []
    for config in report_configs:
        task = generate_report.s(
            report_id=config["report_id"],
            report_type=config["report_type"],
            investigation_ids=config["investigation_ids"],
            config=config.get("config")
        )
        tasks.append(task)
    
    # Execute in parallel
    job = group(tasks)
    results = job.apply_async()
    
    # Wait for results
    report_results = results.get(timeout=1800)  # 30 minutes timeout
    
    # Aggregate results
    summary = {
        "total_reports": len(report_configs),
        "completed": sum(1 for r in report_results if r.get("status") == "completed"),
        "failed": sum(1 for r in report_results if r.get("status") == "failed"),
        "total_words": sum(r.get("word_count", 0) for r in report_results),
        "results": report_results
    }
    
    logger.info(
        "batch_report_generation_completed",
        total=summary["total_reports"],
        completed=summary["completed"]
    )
    
    return summary


@priority_task(priority=TaskPriority.HIGH)
def generate_urgent_report(
    investigation_id: str,
    reason: str,
    recipients: List[str]
) -> Dict[str, Any]:
    """
    Generate urgent report with notifications.
    
    Args:
        investigation_id: Investigation to report on
        reason: Reason for urgency
        recipients: Email recipients for notification
        
    Returns:
        Report generation results
    """
    logger.warning(
        "urgent_report_requested",
        investigation_id=investigation_id,
        reason=reason,
        recipients=len(recipients)
    )
    
    # Generate report with high priority
    report_id = f"URGENT-{datetime.now().strftime('%Y%m%d%H%M%S')}"
    
    # Chain tasks: generate report → export to PDF → send notifications
    workflow = chain(
        generate_report.s(
            report_id=report_id,
            report_type="urgent",
            investigation_ids=[investigation_id],
            config={"reason": reason, "priority": "urgent"}
        ),
        export_report_to_pdf.s(),
        send_report_notifications.s(recipients=recipients)
    )
    
    result = workflow.apply_async(priority=9)
    return result.get()


@celery_app.task(name="tasks.export_report_to_pdf", queue="normal")
def export_report_to_pdf(report_data: Dict[str, Any]) -> Dict[str, Any]:
    """Export report to PDF format."""
    try:
        export_service = ExportService()
        
        pdf_content = asyncio.run(
            export_service.generate_pdf(
                content=report_data.get("content", ""),
                title=report_data.get("title", "Report"),
                metadata=report_data
            )
        )
        
        return {
            **report_data,
            "pdf_size": len(pdf_content),
            "pdf_generated": True
        }
        
    except Exception as e:
        logger.error(
            "pdf_export_failed",
            report_id=report_data.get("report_id"),
            error=str(e)
        )
        raise


@celery_app.task(name="tasks.send_report_notifications", queue="high")
def send_report_notifications(
    report_data: Dict[str, Any],
    recipients: List[str]
) -> Dict[str, Any]:
    """Send report notifications."""
    logger.info(
        "sending_notifications",
        report_id=report_data.get("report_id"),
        recipients=len(recipients)
    )
    
    # In production, this would send actual emails
    # For now, just log the action
    
    return {
        "report_id": report_data.get("report_id"),
        "notifications_sent": len(recipients),
        "timestamp": datetime.now().isoformat()
    }