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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Report Generator Module - Generate HTML and JSON reports.

This module handles:
- HTML dashboard generation
- JSON report output
- Console summary formatting
- Report history management
"""

import json
import logging
from pathlib import Path
from typing import Dict, Any
from datetime import datetime

logger = logging.getLogger(__name__)


class ReportGenerator:
    """Generate quality reports in multiple formats."""
    
    def __init__(self, config: Dict[str, Any], project_root: str):
        """
        Initialize the report generator.
        
        Args:
            config: Configuration dictionary for report generator
            project_root: Root directory of the project
        """
        self.config = config
        self.project_root = Path(project_root)
        self.output_dir = self.project_root / config.get("output_dir", "quality_reports")
        self.output_dir.mkdir(exist_ok=True)
    
    def generate_reports(self, results: Dict[str, Any]):
        """
        Generate all configured reports.
        
        Args:
            results: Combined results from all modules
        """
        logger.info("Generating reports...")
        
        # Generate JSON report
        if self.config.get("generate_json", True):
            self._generate_json_report(results)
        
        # Generate HTML report
        if self.config.get("generate_html", True):
            self._generate_html_report(results)
        
        # Generate console summary
        if self.config.get("generate_console_summary", True):
            self._print_console_summary(results)
        
        # Save to history
        if self.config.get("save_history", True):
            self._save_to_history(results)
        
        logger.info(f"Reports generated in: {self.output_dir}")
    
    def _generate_json_report(self, results: Dict[str, Any]):
        """Generate JSON report."""
        logger.info("Generating JSON report...")
        
        report_file = self.output_dir / "latest_report.json"
        
        report_data = {
            "timestamp": datetime.now().isoformat(),
            "project_name": self.config.get("project_name", "Unknown"),
            "results": results
        }
        
        with open(report_file, 'w') as f:
            json.dump(report_data, f, indent=2)
        
        logger.info(f"JSON report saved to: {report_file}")
    
    def _generate_html_report(self, results: Dict[str, Any]):
        """Generate HTML dashboard report."""
        logger.info("Generating HTML report...")
        
        report_file = self.output_dir / "latest_report.html"
        
        # Extract results
        cleaner = results.get("code_cleaner", {})
        vuln = results.get("vulnerability_checker", {})
        conn = results.get("connection_tester", {})
        tester = results.get("code_tester", {})
        
        # Build HTML
        html = f"""<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Code Quality Report - {self.config.get('project_name', 'Project')}</title>
    <style>
        * {{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }}
        
        body {{
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 20px;
            min-height: 100vh;
        }}
        
        .container {{
            max-width: 1200px;
            margin: 0 auto;
        }}
        
        .header {{
            background: white;
            padding: 30px;
            border-radius: 10px;
            margin-bottom: 20px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }}
        
        .header h1 {{
            color: #333;
            margin-bottom: 10px;
        }}
        
        .header .timestamp {{
            color: #666;
            font-size: 14px;
        }}
        
        .summary-grid {{
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 20px;
        }}
        
        .summary-card {{
            background: white;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }}
        
        .summary-card h3 {{
            color: #333;
            margin-bottom: 15px;
            font-size: 16px;
            text-transform: uppercase;
            letter-spacing: 1px;
        }}
        
        .metric {{
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
            padding: 8px 0;
            border-bottom: 1px solid #eee;
        }}
        
        .metric:last-child {{
            border-bottom: none;
        }}
        
        .metric-label {{
            color: #666;
            font-size: 14px;
        }}
        
        .metric-value {{
            font-weight: bold;
            font-size: 16px;
        }}
        
        .metric-value.success {{
            color: #10b981;
        }}
        
        .metric-value.warning {{
            color: #f59e0b;
        }}
        
        .metric-value.error {{
            color: #ef4444;
        }}
        
        .section {{
            background: white;
            padding: 25px;
            border-radius: 10px;
            margin-bottom: 20px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }}
        
        .section h2 {{
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #667eea;
        }}
        
        .issue-list {{
            list-style: none;
        }}
        
        .issue-item {{
            padding: 15px;
            margin-bottom: 10px;
            border-left: 4px solid #ddd;
            background: #f9fafb;
            border-radius: 4px;
        }}
        
        .issue-item.critical {{
            border-left-color: #dc2626;
            background: #fef2f2;
        }}
        
        .issue-item.high {{
            border-left-color: #f59e0b;
            background: #fffbeb;
        }}
        
        .issue-item.medium {{
            border-left-color: #3b82f6;
            background: #eff6ff;
        }}
        
        .issue-item.low {{
            border-left-color: #10b981;
            background: #f0fdf4;
        }}
        
        .issue-severity {{
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
            margin-right: 10px;
        }}
        
        .severity-critical {{
            background: #dc2626;
            color: white;
        }}
        
        .severity-high {{
            background: #f59e0b;
            color: white;
        }}
        
        .severity-medium {{
            background: #3b82f6;
            color: white;
        }}
        
        .severity-low {{
            background: #10b981;
            color: white;
        }}
        
        .test-result {{
            padding: 10px;
            margin-bottom: 8px;
            border-radius: 4px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }}
        
        .test-result.passed {{
            background: #f0fdf4;
            border-left: 4px solid #10b981;
        }}
        
        .test-result.failed {{
            background: #fef2f2;
            border-left: 4px solid #ef4444;
        }}
        
        .status-badge {{
            padding: 4px 12px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }}
        
        .status-passed {{
            background: #10b981;
            color: white;
        }}
        
        .status-failed {{
            background: #ef4444;
            color: white;
        }}
        
        .footer {{
            text-align: center;
            color: white;
            margin-top: 30px;
            padding: 20px;
        }}
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>πŸ” Code Quality Report</h1>
            <p class="timestamp">Generated: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}</p>
            <p class="timestamp">Project: {self.config.get('project_name', 'Unknown')}</p>
        </div>
        
        <div class="summary-grid">
            <div class="summary-card">
                <h3>πŸ“ Code Cleaning</h3>
                <div class="metric">
                    <span class="metric-label">Files Processed</span>
                    <span class="metric-value">{cleaner.get('total_files_processed', 0)}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Formatted</span>
                    <span class="metric-value success">{len(cleaner.get('formatted_files', []))}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Errors</span>
                    <span class="metric-value {'error' if cleaner.get('errors') else 'success'}">{len(cleaner.get('errors', []))}</span>
                </div>
            </div>
            
            <div class="summary-card">
                <h3>πŸ”’ Security</h3>
                <div class="metric">
                    <span class="metric-label">Total Issues</span>
                    <span class="metric-value {'error' if vuln.get('total_issues', 0) > 0 else 'success'}">{vuln.get('total_issues', 0)}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Critical</span>
                    <span class="metric-value error">{vuln.get('severity_counts', {}).get('CRITICAL', 0)}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">High</span>
                    <span class="metric-value warning">{vuln.get('severity_counts', {}).get('HIGH', 0)}</span>
                </div>
            </div>
            
            <div class="summary-card">
                <h3>πŸ”Œ Connections</h3>
                <div class="metric">
                    <span class="metric-label">Total Tests</span>
                    <span class="metric-value">{conn.get('total_tests', 0)}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Passed</span>
                    <span class="metric-value success">{conn.get('passed_tests', 0)}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Failed</span>
                    <span class="metric-value {'error' if conn.get('failed_tests', 0) > 0 else 'success'}">{conn.get('failed_tests', 0)}</span>
                </div>
            </div>
            
            <div class="summary-card">
                <h3>βœ… Tests</h3>
                <div class="metric">
                    <span class="metric-label">Total Tests</span>
                    <span class="metric-value">{tester.get('total_tests', 0)}</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Coverage</span>
                    <span class="metric-value {'success' if tester.get('coverage_percentage', 0) >= 70 else 'warning'}">{tester.get('coverage_percentage', 0):.1f}%</span>
                </div>
                <div class="metric">
                    <span class="metric-label">Pylint Score</span>
                    <span class="metric-value">{tester.get('pylint_results', {}).get('score', 'N/A')}/10</span>
                </div>
            </div>
        </div>
        
        <div class="section">
            <h2>πŸ”’ Security Vulnerabilities</h2>
            {self._generate_vulnerability_html(vuln)}
        </div>
        
        <div class="section">
            <h2>πŸ”Œ Connection Tests</h2>
            {self._generate_connection_html(conn)}
        </div>
        
        <div class="footer">
            <p>Generated by Code Quality Agent</p>
        </div>
    </div>
</body>
</html>"""
        
        with open(report_file, 'w', encoding='utf-8') as f:
            f.write(html)
        
        logger.info(f"HTML report saved to: {report_file}")
    
    def _generate_vulnerability_html(self, vuln: Dict[str, Any]) -> str:
        """Generate HTML for vulnerability section."""
        if vuln.get('total_issues', 0) == 0:
            return '<p style="color: #10b981; font-weight: bold;">βœ“ No security issues found!</p>'
        
        html = '<ul class="issue-list">'
        
        # Bandit issues
        for issue in vuln.get('bandit_issues', [])[:10]:  # Limit to 10
            severity = issue.get('severity', 'LOW').lower()
            html += f'''
            <li class="issue-item {severity}">
                <span class="issue-severity severity-{severity}">{issue.get('severity', 'LOW')}</span>
                <strong>{issue.get('issue', 'Unknown issue')}</strong>
                <br><small>{issue.get('file', '')}:{issue.get('line', '')}</small>
            </li>
            '''
        
        # Custom checks
        for issue in vuln.get('custom_checks', []):
            severity = issue.get('severity', 'LOW').lower()
            html += f'''
            <li class="issue-item {severity}">
                <span class="issue-severity severity-{severity}">{issue.get('severity', 'LOW')}</span>
                <strong>{issue.get('issue', 'Unknown issue')}</strong>
                <br><small>{issue.get('file', '')}</small>
                <br><small style="color: #666;">{issue.get('recommendation', '')}</small>
            </li>
            '''
        
        html += '</ul>'
        return html
    
    def _generate_connection_html(self, conn: Dict[str, Any]) -> str:
        """Generate HTML for connection tests section."""
        if conn.get('total_tests', 0) == 0:
            return '<p style="color: #666;">No connection tests were run.</p>'
        
        html = ''
        
        # Database tests
        for test in conn.get('database_tests', []):
            status = test.get('status', 'FAILED').lower()
            html += f'''
            <div class="test-result {status}">
                <span>{test.get('test', 'Database Test')}</span>
                <span class="status-badge status-{status}">{test.get('status', 'UNKNOWN')}</span>
            </div>
            '''
        
        # API tests
        for test in conn.get('api_tests', [])[:5]:  # Limit to 5
            status = test.get('status', 'FAILED').lower()
            html += f'''
            <div class="test-result {status}">
                <span>{test.get('test', 'API Test')}</span>
                <span class="status-badge status-{status}">{test.get('status', 'UNKNOWN')}</span>
            </div>
            '''
        
        return html
    
    def _print_console_summary(self, results: Dict[str, Any]):
        """Print a summary to console."""
        print("\n" + "="*70)
        print("CODE QUALITY REPORT SUMMARY")
        print("="*70)
        
        # Code Cleaner
        cleaner = results.get("code_cleaner", {})
        print(f"\nπŸ“ CODE CLEANING:")
        print(f"   Files processed: {cleaner.get('total_files_processed', 0)}")
        print(f"   Formatted: {len(cleaner.get('formatted_files', []))}")
        
        # Vulnerabilities
        vuln = results.get("vulnerability_checker", {})
        print(f"\nπŸ”’ SECURITY:")
        print(f"   Total issues: {vuln.get('total_issues', 0)}")
        print(f"   Critical: {vuln.get('severity_counts', {}).get('CRITICAL', 0)}")
        print(f"   High: {vuln.get('severity_counts', {}).get('HIGH', 0)}")
        
        # Connections
        conn = results.get("connection_tester", {})
        print(f"\nπŸ”Œ CONNECTIONS:")
        print(f"   Tests run: {conn.get('total_tests', 0)}")
        print(f"   Passed: {conn.get('passed_tests', 0)}")
        print(f"   Failed: {conn.get('failed_tests', 0)}")
        
        # Tests
        tester = results.get("code_tester", {})
        print(f"\nβœ… CODE TESTS:")
        print(f"   Total tests: {tester.get('total_tests', 0)}")
        print(f"   Coverage: {tester.get('coverage_percentage', 0):.1f}%")
        print(f"   Pylint score: {tester.get('pylint_results', {}).get('score', 'N/A')}/10")
        
        print("\n" + "="*70)
        print(f"Full reports available in: {self.output_dir}")
        print("="*70 + "\n")
    
    def _save_to_history(self, results: Dict[str, Any]):
        """Save report to history."""
        history_dir = self.output_dir / "history"
        history_dir.mkdir(exist_ok=True)
        
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        history_file = history_dir / f"report_{timestamp}.json"
        
        with open(history_file, 'w') as f:
            json.dump(results, f, indent=2)
        
        # Clean old history files
        max_history = self.config.get("max_history_reports", 10)
        history_files = sorted(history_dir.glob("report_*.json"))
        
        if len(history_files) > max_history:
            for old_file in history_files[:-max_history]:
                old_file.unlink()