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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
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
"""
Admin UI Backend Endpoints
Administrative controls for system oversight and review management

Features:
- Review queue management
- System configuration
- User management (placeholder)
- Performance monitoring dashboard
- Compliance reporting interface
- Model versioning controls

Author: MiniMax Agent
Date: 2025-10-29
Version: 1.0.0
"""

from fastapi import APIRouter, HTTPException, Depends
from typing import Dict, List, Any, Optional
from datetime import datetime, timedelta
from pydantic import BaseModel

from monitoring_service import get_monitoring_service
from model_versioning import get_versioning_system
from production_logging import get_medical_logger
from compliance_reporting import get_compliance_system


# Create admin router
admin_router = APIRouter(prefix="/admin", tags=["admin"])


# ================================
# REQUEST/RESPONSE MODELS
# ================================

class ReviewQueueItem(BaseModel):
    """Review queue item"""
    item_id: str
    document_id: str
    document_type: str
    confidence_score: float
    risk_level: str
    created_at: str
    assigned_to: Optional[str] = None
    priority: str  # "critical", "high", "medium", "low"


class ReviewAction(BaseModel):
    """Review action request"""
    item_id: str
    reviewer_id: str
    action: str  # "approve", "reject", "escalate"
    comments: Optional[str] = None


class SystemConfiguration(BaseModel):
    """System configuration"""
    error_threshold: float = 0.05
    cache_size_mb: int = 1000
    cache_ttl_hours: int = 24
    alert_email: Optional[str] = None


class ModelDeployment(BaseModel):
    """Model deployment request"""
    model_id: str
    version: str
    set_active: bool = False


# ================================
# REVIEW QUEUE ENDPOINTS
# ================================

# In-memory review queue (in production, use database)
review_queue: List[ReviewQueueItem] = []


@admin_router.get("/review-queue")
async def get_review_queue(
    priority: Optional[str] = None,
    status: Optional[str] = None
) -> Dict[str, Any]:
    """Get current review queue"""
    
    filtered_queue = review_queue
    
    if priority:
        filtered_queue = [item for item in filtered_queue if item.priority == priority]
    
    return {
        "total_items": len(review_queue),
        "filtered_items": len(filtered_queue),
        "queue": [item.dict() for item in filtered_queue],
        "summary": {
            "critical": len([i for i in review_queue if i.priority == "critical"]),
            "high": len([i for i in review_queue if i.priority == "high"]),
            "medium": len([i for i in review_queue if i.priority == "medium"]),
            "low": len([i for i in review_queue if i.priority == "low"])
        }
    }


@admin_router.post("/review-queue/action")
async def submit_review_action(action: ReviewAction) -> Dict[str, Any]:
    """Submit review action (approve/reject/escalate)"""
    
    # Find item in queue
    item = next((i for i in review_queue if i.item_id == action.item_id), None)
    
    if not item:
        raise HTTPException(status_code=404, detail="Review item not found")
    
    # Log review action
    logger = get_medical_logger()
    logger.info(
        f"Review action: {action.action} on {action.item_id}",
        user_id=action.reviewer_id,
        document_id=item.document_id,
        details={"action": action.action, "comments": action.comments}
    )
    
    # Log to compliance system
    compliance = get_compliance_system()
    compliance.log_audit_event(
        user_id=action.reviewer_id,
        event_type="REVIEW",
        resource=f"document:{item.document_id}",
        action=action.action.upper(),
        ip_address="internal",
        details={"item_id": action.item_id, "comments": action.comments}
    )
    
    # Remove from queue if approved or rejected
    if action.action in ["approve", "reject"]:
        review_queue.remove(item)
    
    return {
        "success": True,
        "action": action.action,
        "item_id": action.item_id,
        "message": f"Review {action.action}d successfully"
    }


@admin_router.post("/review-queue/assign")
async def assign_review(
    item_id: str,
    reviewer_id: str
) -> Dict[str, Any]:
    """Assign review to a reviewer"""
    
    item = next((i for i in review_queue if i.item_id == item_id), None)
    
    if not item:
        raise HTTPException(status_code=404, detail="Review item not found")
    
    item.assigned_to = reviewer_id
    
    return {
        "success": True,
        "item_id": item_id,
        "assigned_to": reviewer_id
    }


# ================================
# MONITORING DASHBOARD ENDPOINTS
# ================================

@admin_router.get("/dashboard")
async def get_admin_dashboard() -> Dict[str, Any]:
    """Get comprehensive admin dashboard data"""
    
    monitoring = get_monitoring_service()
    versioning = get_versioning_system()
    compliance = get_compliance_system()
    
    return {
        "timestamp": datetime.utcnow().isoformat(),
        "system_health": monitoring.get_system_health(),
        "performance_dashboard": monitoring.get_performance_dashboard(),
        "model_inventory": versioning.get_system_status(),
        "compliance_dashboard": compliance.get_compliance_dashboard(),
        "review_queue_summary": {
            "total_items": len(review_queue),
            "critical_items": len([i for i in review_queue if i.priority == "critical"]),
            "unassigned_items": len([i for i in review_queue if not i.assigned_to])
        }
    }


@admin_router.get("/metrics/performance")
async def get_performance_metrics(
    window_minutes: int = 60
) -> Dict[str, Any]:
    """Get detailed performance metrics"""
    
    monitoring = get_monitoring_service()
    
    # Get statistics for key stages
    stages = ["pdf_processing", "classification", "model_routing", "synthesis"]
    
    performance_data = {}
    for stage in stages:
        stats = monitoring.latency_tracker.get_stage_statistics(stage, window_minutes)
        performance_data[stage] = stats
    
    error_summary = monitoring.error_monitor.get_error_summary()
    
    return {
        "window_minutes": window_minutes,
        "latency_by_stage": performance_data,
        "error_summary": error_summary,
        "timestamp": datetime.utcnow().isoformat()
    }


@admin_router.get("/metrics/cache")
async def get_cache_metrics() -> Dict[str, Any]:
    """Get cache performance metrics"""
    
    versioning = get_versioning_system()
    cache_stats = versioning.input_cache.get_statistics()
    
    return {
        "cache_statistics": cache_stats,
        "recommendations": _generate_cache_recommendations(cache_stats),
        "timestamp": datetime.utcnow().isoformat()
    }


# ================================
# MODEL MANAGEMENT ENDPOINTS
# ================================

@admin_router.get("/models/inventory")
async def get_model_inventory() -> Dict[str, Any]:
    """Get complete model inventory"""
    
    versioning = get_versioning_system()
    inventory = versioning.model_registry.get_model_inventory()
    
    return {
        "inventory": inventory,
        "summary": {
            "total_models": len(inventory),
            "total_versions": sum(data["total_versions"] for data in inventory.values())
        },
        "timestamp": datetime.utcnow().isoformat()
    }


@admin_router.post("/models/deploy")
async def deploy_model_version(deployment: ModelDeployment) -> Dict[str, Any]:
    """Deploy a model version"""
    
    versioning = get_versioning_system()
    
    try:
        if deployment.set_active:
            versioning.model_registry.set_active_version(
                deployment.model_id,
                deployment.version
            )
            
            # Invalidate cache for this model
            versioning.input_cache.invalidate_model_version(deployment.version)
        
        return {
            "success": True,
            "model_id": deployment.model_id,
            "version": deployment.version,
            "active": deployment.set_active,
            "message": f"Model {deployment.model_id} v{deployment.version} deployed"
        }
    
    except Exception as e:
        raise HTTPException(status_code=400, detail=str(e))


@admin_router.post("/models/rollback")
async def rollback_model(
    model_id: str,
    version: str
) -> Dict[str, Any]:
    """Rollback to a previous model version"""
    
    versioning = get_versioning_system()
    
    success = versioning.model_registry.rollback_to_version(model_id, version)
    
    if not success:
        raise HTTPException(status_code=404, detail="Model version not found")
    
    # Invalidate cache
    versioning.input_cache.invalidate_model_version(version)
    
    return {
        "success": True,
        "model_id": model_id,
        "rolled_back_to": version,
        "message": f"Rolled back {model_id} to v{version}"
    }


@admin_router.get("/models/compare")
async def compare_model_versions(
    model_id: str,
    version1: str,
    version2: str,
    metric: str = "accuracy"
) -> Dict[str, Any]:
    """Compare two model versions"""
    
    versioning = get_versioning_system()
    comparison = versioning.model_registry.compare_versions(
        model_id, version1, version2, metric
    )
    
    return comparison


# ================================
# COMPLIANCE ENDPOINTS
# ================================

@admin_router.get("/compliance/hipaa-report")
async def get_hipaa_report(
    days: int = 30
) -> Dict[str, Any]:
    """Generate HIPAA compliance report"""
    
    compliance = get_compliance_system()
    
    end_date = datetime.utcnow()
    start_date = end_date - timedelta(days=days)
    
    report = compliance.generate_hipaa_report(start_date, end_date)
    
    return report


@admin_router.get("/compliance/gdpr-report")
async def get_gdpr_report(
    days: int = 30
) -> Dict[str, Any]:
    """Generate GDPR compliance report"""
    
    compliance = get_compliance_system()
    
    end_date = datetime.utcnow()
    start_date = end_date - timedelta(days=days)
    
    report = compliance.generate_gdpr_report(start_date, end_date)
    
    return report


@admin_router.get("/compliance/quality-metrics")
async def get_quality_metrics(
    days: int = 30
) -> Dict[str, Any]:
    """Get clinical quality metrics"""
    
    compliance = get_compliance_system()
    report = compliance.generate_quality_metrics_report(days)
    
    return report


@admin_router.get("/compliance/security-incidents")
async def get_security_incidents(
    days: int = 30
) -> Dict[str, Any]:
    """Get security incidents report"""
    
    compliance = get_compliance_system()
    report = compliance.generate_security_incidents_report(days)
    
    return report


# ================================
# SYSTEM CONFIGURATION ENDPOINTS
# ================================

# In-memory configuration (in production, use database)
system_config = SystemConfiguration()


@admin_router.get("/config")
async def get_system_configuration() -> SystemConfiguration:
    """Get current system configuration"""
    return system_config


@admin_router.post("/config")
async def update_system_configuration(
    config: SystemConfiguration
) -> Dict[str, Any]:
    """Update system configuration"""
    
    global system_config
    system_config = config
    
    logger = get_medical_logger()
    logger.info(
        "System configuration updated",
        details=config.dict()
    )
    
    return {
        "success": True,
        "config": config.dict(),
        "message": "System configuration updated"
    }


@admin_router.post("/cache/clear")
async def clear_cache() -> Dict[str, Any]:
    """Clear all cache entries"""
    
    versioning = get_versioning_system()
    versioning.input_cache.clear()
    
    return {
        "success": True,
        "message": "Cache cleared successfully"
    }


# ================================
# ALERTS MANAGEMENT
# ================================

@admin_router.get("/alerts")
async def get_active_alerts(
    level: Optional[str] = None
) -> Dict[str, Any]:
    """Get active system alerts"""
    
    monitoring = get_monitoring_service()
    
    from monitoring_service import AlertLevel
    
    alert_level = None
    if level:
        alert_level = AlertLevel(level.upper())
    
    alerts = monitoring.alert_manager.get_active_alerts(level=alert_level)
    summary = monitoring.alert_manager.get_alert_summary()
    
    return {
        "active_alerts": [a.to_dict() for a in alerts],
        "summary": summary,
        "timestamp": datetime.utcnow().isoformat()
    }


@admin_router.post("/alerts/{alert_id}/resolve")
async def resolve_alert(alert_id: str) -> Dict[str, Any]:
    """Resolve an active alert"""
    
    monitoring = get_monitoring_service()
    monitoring.alert_manager.resolve_alert(alert_id)
    
    return {
        "success": True,
        "alert_id": alert_id,
        "message": "Alert resolved"
    }


# ================================
# CACHE MANAGEMENT ENDPOINTS
# ================================

@admin_router.get("/cache/statistics")
async def get_cache_statistics() -> Dict[str, Any]:
    """
    Get comprehensive cache statistics
    
    Returns cache performance metrics including:
    - Hit/miss rates
    - Memory usage
    - Entry count
    - Eviction statistics
    """
    
    monitoring = get_monitoring_service()
    cache_stats = monitoring.get_cache_statistics()
    
    return {
        "statistics": cache_stats,
        "recommendations": _generate_cache_recommendations_v2(cache_stats),
        "timestamp": datetime.utcnow().isoformat()
    }


@admin_router.get("/cache/entries")
async def list_cache_entries(limit: int = 100) -> Dict[str, Any]:
    """
    List cache entries with metadata
    
    Args:
        limit: Maximum number of entries to return (default: 100)
    """
    
    monitoring = get_monitoring_service()
    entries = monitoring.cache_service.list_entries(limit=limit)
    
    return {
        "entries": entries,
        "total_shown": len(entries),
        "timestamp": datetime.utcnow().isoformat()
    }


@admin_router.get("/cache/entry/{key}")
async def get_cache_entry_info(key: str) -> Dict[str, Any]:
    """
    Get detailed information about a specific cache entry
    
    Args:
        key: Cache key (SHA256 fingerprint)
    """
    
    monitoring = get_monitoring_service()
    entry_info = monitoring.cache_service.get_entry_info(key)
    
    if entry_info is None:
        raise HTTPException(status_code=404, detail="Cache entry not found")
    
    return entry_info


@admin_router.post("/cache/invalidate/{key}")
async def invalidate_cache_entry(key: str) -> Dict[str, Any]:
    """
    Invalidate a specific cache entry
    
    Args:
        key: Cache key (SHA256 fingerprint)
    """
    
    monitoring = get_monitoring_service()
    success = monitoring.cache_service.invalidate(key)
    
    if not success:
        raise HTTPException(status_code=404, detail="Cache entry not found")
    
    return {
        "success": True,
        "key": key,
        "message": "Cache entry invalidated"
    }


@admin_router.post("/cache/clear")
async def clear_cache() -> Dict[str, Any]:
    """
    Clear all cache entries
    
    WARNING: This will clear all cached data and may temporarily impact performance
    """
    
    monitoring = get_monitoring_service()
    monitoring.cache_service.clear()
    
    return {
        "success": True,
        "message": "All cache entries cleared",
        "timestamp": datetime.utcnow().isoformat()
    }


# ================================
# HELPER FUNCTIONS
# ================================

def _generate_cache_recommendations_v2(stats: Dict[str, Any]) -> List[str]:
    """Generate cache optimization recommendations based on statistics"""
    recommendations = []
    
    hit_rate = stats.get("hit_rate", 0.0)
    memory_usage = stats.get("memory_usage_mb", 0.0)
    max_memory = stats.get("max_memory_mb", 512)
    evictions = stats.get("evictions", 0)
    total_entries = stats.get("total_entries", 0)
    
    # Hit rate recommendations
    if hit_rate < 0.5:
        recommendations.append(f"Low cache hit rate ({hit_rate*100:.1f}%). Consider increasing cache size or TTL.")
    elif hit_rate > 0.8:
        recommendations.append(f"Excellent cache hit rate ({hit_rate*100:.1f}%). Cache performing optimally.")
    
    # Memory recommendations
    utilization = (memory_usage / max_memory) * 100 if max_memory > 0 else 0
    if utilization > 90:
        recommendations.append(f"Cache near capacity ({utilization:.1f}% used). Consider increasing max cache size.")
    
    # Eviction recommendations
    if total_entries > 0 and evictions > total_entries * 0.1:
        recommendations.append(f"High eviction rate ({evictions} evictions). Increase cache size to improve performance.")
    
    # Default message
    if not recommendations:
        recommendations.append("Cache performing within normal parameters.")
    
    return recommendations

def _generate_cache_recommendations(stats: Dict[str, Any]) -> List[str]:
    """Generate cache optimization recommendations"""
    recommendations = []
    
    if stats["hit_rate_percent"] < 50:
        recommendations.append("Low cache hit rate. Consider increasing cache size or TTL.")
    
    if stats["utilization_percent"] > 90:
        recommendations.append("Cache near capacity. Consider increasing max cache size.")
    
    if stats["evictions"] > stats["total_requests"] * 0.1:
        recommendations.append("High eviction rate. Increase cache size to improve performance.")
    
    if not recommendations:
        recommendations.append("Cache performing optimally.")
    
    return recommendations