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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
"""

System monitoring and health check API endpoints.



This module implements REST API endpoints for system monitoring,

health checks, metrics collection, and queue status monitoring.

"""

import logging
import asyncio
from datetime import datetime, timedelta
from typing import Dict, Any, Optional, List
import psutil
import os

from fastapi import APIRouter, Depends, HTTPException, status, Request
from redis.asyncio import Redis

from ...core.redis import get_redis, redis_manager, RedisKeyManager
from ...core.auth import clerk_manager
from ...core.cache import cache_response, CacheConfig, cache_manager
from ...core.cache_monitoring import cache_monitor, generate_cache_report
from ...models.system import SystemHealthResponse, SystemMetricsResponse, QueueStatusResponse
from ...api.dependencies import get_optional_user

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/system", tags=["system"])


@router.get("/health", response_model=SystemHealthResponse)
@cache_response(ttl=CacheConfig.SHORT_TTL, user_specific=True)
async def get_system_health(

    request: Request,

    redis_client: Redis = Depends(get_redis),

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> SystemHealthResponse:
    """

    Get comprehensive system health status.

    

    This endpoint checks the health of all system components including

    Redis, authentication service, job queue, and basic system resources.

    

    Args:

        redis_client: Redis client dependency

        current_user: Optional authenticated user (for detailed info)

        

    Returns:

        SystemHealthResponse with health status of all components

    """
    try:
        logger.info("Performing system health check")
        
        # Initialize health status
        overall_healthy = True
        components = {}
        
        # Check Redis health
        redis_health = await redis_manager.health_check()
        components["redis"] = redis_health
        if redis_health["status"] != "healthy":
            overall_healthy = False
        
        # Check Clerk authentication health
        clerk_health = clerk_manager.health_check()
        components["authentication"] = clerk_health
        if clerk_health["status"] != "healthy":
            overall_healthy = False
        
        # Check job queue health
        queue_health = await _check_queue_health(redis_client)
        components["job_queue"] = queue_health
        if queue_health["status"] != "healthy":
            overall_healthy = False
        
        # Check system resources
        system_health = await _check_system_resources()
        components["system_resources"] = system_health
        if system_health["status"] != "healthy":
            overall_healthy = False
        
        # Check disk space for video storage
        storage_health = await _check_storage_health()
        components["storage"] = storage_health
        if storage_health["status"] != "healthy":
            overall_healthy = False
        
        # Additional checks for authenticated users
        if current_user:
            # Check user-specific health metrics
            user_health = await _check_user_health(current_user["user_info"]["id"], redis_client)
            components["user_context"] = user_health
        
        # Determine overall status
        overall_status = "healthy" if overall_healthy else "unhealthy"
        
        # Calculate uptime
        uptime_seconds = _get_system_uptime()
        
        logger.info(
            "System health check completed",
            overall_status=overall_status,
            components_checked=len(components)
        )
        
        return SystemHealthResponse(
            status=overall_status,
            timestamp=datetime.utcnow(),
            uptime_seconds=uptime_seconds,
            components=components,
            version=os.getenv("APP_VERSION", "unknown"),
            environment=os.getenv("ENVIRONMENT", "unknown")
        )
        
    except Exception as e:
        logger.error(
            f"System health check failed: {str(e)}",
            exc_info=True
        )
        
        return SystemHealthResponse(
            status="unhealthy",
            timestamp=datetime.utcnow(),
            uptime_seconds=0,
            components={
                "error": {
                    "status": "unhealthy",
                    "error": f"Health check failed: {str(e)}"
                }
            },
            version=os.getenv("APP_VERSION", "unknown"),
            environment=os.getenv("ENVIRONMENT", "unknown")
        )


@router.get("/metrics", response_model=SystemMetricsResponse)
@cache_response(ttl=CacheConfig.MEDIUM_TTL, user_specific=True)
async def get_system_metrics(

    request: Request,

    redis_client: Redis = Depends(get_redis),

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> SystemMetricsResponse:
    """

    Get comprehensive system performance metrics.

    

    This endpoint returns detailed system metrics including resource usage,

    job statistics, performance indicators, and operational metrics.

    

    Args:

        redis_client: Redis client dependency

        current_user: Optional authenticated user (for user-specific metrics)

        

    Returns:

        SystemMetricsResponse with system performance metrics

    """
    try:
        logger.info("Collecting system metrics")
        
        # Collect system resource metrics
        cpu_percent = psutil.cpu_percent(interval=1)
        memory = psutil.virtual_memory()
        disk = psutil.disk_usage('/')
        
        # Collect Redis metrics
        redis_info = await redis_client.info()
        redis_memory = redis_info.get("used_memory", 0)
        redis_connections = redis_info.get("connected_clients", 0)
        
        # Collect job queue metrics
        queue_length = await redis_client.llen(RedisKeyManager.JOB_QUEUE)
        
        # Count jobs by status
        job_stats = await _get_job_statistics(redis_client)
        
        # Calculate processing metrics
        processing_metrics = await _get_processing_metrics(redis_client)
        
        # Get error rates
        error_metrics = await _get_error_metrics(redis_client)
        
        # User-specific metrics if authenticated
        user_metrics = None
        if current_user:
            user_metrics = await _get_user_metrics(current_user["user_info"]["id"], redis_client)
        
        logger.info(
            "System metrics collected",
            cpu_percent=cpu_percent,
            memory_percent=memory.percent,
            queue_length=queue_length
        )
        
        return SystemMetricsResponse(
            timestamp=datetime.utcnow(),
            system_resources={
                "cpu_percent": cpu_percent,
                "memory_total_gb": round(memory.total / (1024**3), 2),
                "memory_used_gb": round(memory.used / (1024**3), 2),
                "memory_percent": memory.percent,
                "disk_total_gb": round(disk.total / (1024**3), 2),
                "disk_used_gb": round(disk.used / (1024**3), 2),
                "disk_percent": round((disk.used / disk.total) * 100, 2)
            },
            redis_metrics={
                "memory_used_mb": round(redis_memory / (1024**2), 2),
                "connected_clients": redis_connections,
                "commands_processed": redis_info.get("total_commands_processed", 0),
                "keyspace_hits": redis_info.get("keyspace_hits", 0),
                "keyspace_misses": redis_info.get("keyspace_misses", 0)
            },
            job_metrics={
                "queue_length": queue_length,
                "jobs_by_status": job_stats,
                "processing_metrics": processing_metrics
            },
            performance_metrics={
                "error_rate_percent": error_metrics.get("error_rate", 0),
                "avg_response_time_ms": processing_metrics.get("avg_response_time", 0),
                "requests_per_minute": processing_metrics.get("requests_per_minute", 0)
            },
            user_metrics=user_metrics
        )
        
    except Exception as e:
        logger.error(
            "Failed to collect system metrics",
            error=str(e),
            exc_info=True
        )
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to collect system metrics: {str(e)}"
        )


@router.get("/queue-status", response_model=QueueStatusResponse)
@cache_response(ttl=CacheConfig.SHORT_TTL, user_specific=True)
async def get_queue_status(

    request: Request,

    redis_client: Redis = Depends(get_redis),

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> QueueStatusResponse:
    """

    Get detailed job queue status and monitoring information.

    

    This endpoint provides comprehensive information about the job processing

    queue, including queue length, processing rates, and job distribution.

    

    Args:

        redis_client: Redis client dependency

        current_user: Optional authenticated user

        

    Returns:

        QueueStatusResponse with queue status and metrics

    """
    try:
        logger.info("Collecting queue status")
        
        # Get basic queue metrics
        queue_length = await redis_client.llen(RedisKeyManager.JOB_QUEUE)
        
        # Get queue processing statistics
        processing_stats = await _get_queue_processing_stats(redis_client)
        
        # Get job distribution by priority and type
        job_distribution = await _get_job_distribution(redis_client)
        
        # Calculate estimated wait times
        estimated_wait_times = await _calculate_wait_times(redis_client, queue_length)
        
        # Get recent queue activity
        recent_activity = await _get_recent_queue_activity(redis_client)
        
        # User-specific queue info if authenticated
        user_queue_info = None
        if current_user:
            user_queue_info = await _get_user_queue_info(
                current_user["user_info"]["id"], 
                redis_client
            )
        
        logger.info(
            "Queue status collected",
            queue_length=queue_length,
            processing_jobs=processing_stats.get("processing_count", 0)
        )
        
        return QueueStatusResponse(
            timestamp=datetime.utcnow(),
            queue_length=queue_length,
            processing_jobs=processing_stats.get("processing_count", 0),
            completed_jobs_today=processing_stats.get("completed_today", 0),
            failed_jobs_today=processing_stats.get("failed_today", 0),
            average_processing_time_minutes=processing_stats.get("avg_processing_time", 0),
            job_distribution=job_distribution,
            estimated_wait_times=estimated_wait_times,
            recent_activity=recent_activity,
            user_queue_info=user_queue_info
        )
        
    except Exception as e:
        logger.error(
            "Failed to get queue status",
            error=str(e),
            exc_info=True
        )
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to get queue status: {str(e)}"
        )


# Helper functions for health checks and metrics

async def _check_queue_health(redis_client: Redis) -> Dict[str, Any]:
    """Check job queue health."""
    try:
        queue_length = await redis_client.llen(RedisKeyManager.JOB_QUEUE)
        
        # Consider queue unhealthy if it's too long (>1000 jobs)
        if queue_length > 1000:
            return {
                "status": "unhealthy",
                "queue_length": queue_length,
                "error": "Queue length exceeds healthy threshold"
            }
        
        return {
            "status": "healthy",
            "queue_length": queue_length
        }
        
    except Exception as e:
        return {
            "status": "unhealthy",
            "error": str(e)
        }


async def _check_system_resources() -> Dict[str, Any]:
    """Check system resource health."""
    try:
        cpu_percent = psutil.cpu_percent(interval=1)
        memory = psutil.virtual_memory()
        
        # Consider unhealthy if CPU > 90% or memory > 90%
        if cpu_percent > 90 or memory.percent > 90:
            return {
                "status": "unhealthy",
                "cpu_percent": cpu_percent,
                "memory_percent": memory.percent,
                "error": "High resource usage detected"
            }
        
        return {
            "status": "healthy",
            "cpu_percent": cpu_percent,
            "memory_percent": memory.percent
        }
        
    except Exception as e:
        return {
            "status": "unhealthy",
            "error": str(e)
        }


async def _check_storage_health() -> Dict[str, Any]:
    """Check storage health."""
    try:
        disk = psutil.disk_usage('/')
        disk_percent = (disk.used / disk.total) * 100
        
        # Consider unhealthy if disk usage > 85%
        if disk_percent > 85:
            return {
                "status": "unhealthy",
                "disk_percent": disk_percent,
                "error": "Low disk space"
            }
        
        return {
            "status": "healthy",
            "disk_percent": disk_percent,
            "free_gb": round((disk.total - disk.used) / (1024**3), 2)
        }
        
    except Exception as e:
        return {
            "status": "unhealthy",
            "error": str(e)
        }


async def _check_user_health(user_id: str, redis_client: Redis) -> Dict[str, Any]:
    """Check user-specific health metrics."""
    try:
        # Get user's active jobs
        user_jobs_key = RedisKeyManager.user_jobs_key(user_id)
        job_count = await redis_client.scard(user_jobs_key)
        
        return {
            "status": "healthy",
            "active_jobs": job_count
        }
        
    except Exception as e:
        return {
            "status": "unhealthy",
            "error": str(e)
        }


async def _get_job_statistics(redis_client: Redis) -> Dict[str, int]:
    """Get job statistics by status."""
    try:
        stats = {
            "queued": 0,
            "processing": 0,
            "completed": 0,
            "failed": 0,
            "cancelled": 0
        }
        
        # This is a simplified implementation
        # In a real system, you might maintain counters or scan all jobs
        queue_length = await redis_client.llen(RedisKeyManager.JOB_QUEUE)
        stats["queued"] = queue_length
        
        return stats
        
    except Exception as e:
        logger.error(f"Failed to get job statistics: {e}")
        return {}


async def _get_processing_metrics(redis_client: Redis) -> Dict[str, float]:
    """Get processing performance metrics."""
    try:
        # This would typically be collected from application metrics
        # For now, return placeholder values
        return {
            "avg_response_time": 150.0,  # ms
            "requests_per_minute": 45.0,
            "success_rate": 98.5  # %
        }
        
    except Exception as e:
        logger.error(f"Failed to get processing metrics: {e}")
        return {}


async def _get_error_metrics(redis_client: Redis) -> Dict[str, float]:
    """Get error rate metrics."""
    try:
        # This would typically be collected from application logs/metrics
        return {
            "error_rate": 1.5,  # %
            "errors_last_hour": 3
        }
        
    except Exception as e:
        logger.error(f"Failed to get error metrics: {e}")
        return {}


async def _get_user_metrics(user_id: str, redis_client: Redis) -> Dict[str, Any]:
    """Get user-specific metrics."""
    try:
        user_jobs_key = RedisKeyManager.user_jobs_key(user_id)
        job_count = await redis_client.scard(user_jobs_key)
        
        return {
            "total_jobs": job_count,
            "jobs_today": 0,  # Would need to implement date-based counting
            "quota_used": 0,  # Would need to implement quota tracking
            "quota_limit": 100
        }
        
    except Exception as e:
        logger.error(f"Failed to get user metrics: {e}")
        return {}


def _get_system_uptime() -> int:
    """Get system uptime in seconds."""
    try:
        return int(psutil.boot_time())
    except Exception:
        return 0


async def _get_queue_processing_stats(redis_client: Redis) -> Dict[str, Any]:
    """Get queue processing statistics."""
    try:
        # This would typically be maintained as counters
        return {
            "processing_count": 0,
            "completed_today": 0,
            "failed_today": 0,
            "avg_processing_time": 5.0  # minutes
        }
        
    except Exception as e:
        logger.error(f"Failed to get queue processing stats: {e}")
        return {}


async def _get_job_distribution(redis_client: Redis) -> Dict[str, Any]:
    """Get job distribution by priority and type."""
    try:
        return {
            "by_priority": {
                "low": 10,
                "normal": 25,
                "high": 5,
                "urgent": 0
            },
            "by_type": {
                "video_generation": 35,
                "batch_video_generation": 5
            }
        }
        
    except Exception as e:
        logger.error(f"Failed to get job distribution: {e}")
        return {}


async def _calculate_wait_times(redis_client: Redis, queue_length: int) -> Dict[str, float]:
    """Calculate estimated wait times."""
    try:
        # Simple calculation based on queue length and average processing time
        avg_processing_time = 5.0  # minutes
        
        return {
            "next_job_minutes": avg_processing_time,
            "queue_end_minutes": queue_length * avg_processing_time,
            "new_job_minutes": (queue_length + 1) * avg_processing_time
        }
        
    except Exception as e:
        logger.error(f"Failed to calculate wait times: {e}")
        return {}


async def _get_recent_queue_activity(redis_client: Redis) -> List[Dict[str, Any]]:
    """Get recent queue activity."""
    try:
        # This would typically be maintained as a log
        return [
            {
                "timestamp": (datetime.utcnow() - timedelta(minutes=5)).isoformat(),
                "action": "job_completed",
                "job_id": "example-job-1"
            },
            {
                "timestamp": (datetime.utcnow() - timedelta(minutes=10)).isoformat(),
                "action": "job_started",
                "job_id": "example-job-2"
            }
        ]
        
    except Exception as e:
        logger.error(f"Failed to get recent queue activity: {e}")
        return []


async def _get_user_queue_info(user_id: str, redis_client: Redis) -> Dict[str, Any]:
    """Get user-specific queue information."""
    try:
        user_jobs_key = RedisKeyManager.user_jobs_key(user_id)
        user_job_count = await redis_client.scard(user_jobs_key)
        
        return {
            "jobs_in_queue": 0,  # Would need to check which jobs are queued
            "jobs_processing": 0,  # Would need to check which jobs are processing
            "estimated_wait_time_minutes": 0
        }
        
    except Exception as e:
        logger.error(f"Failed to get user queue info: {e}")
        return {}


# Cache management endpoints

@router.get("/cache/info")
@cache_response(ttl=CacheConfig.SHORT_TTL)
async def get_cache_info(

    request: Request,

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """

    Get comprehensive cache information and statistics.

    

    Returns cache performance metrics, Redis memory usage,

    and connection information.

    """
    try:
        from ...core.cache import get_cache_info
        cache_info = await get_cache_info()
        
        # Add monitoring data
        cache_info["monitoring"] = cache_monitor.get_performance_summary()
        
        return cache_info
        
    except Exception as e:
        logger.error(f"Failed to get cache info: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to get cache information: {str(e)}"
        )


@router.get("/cache/metrics")
async def get_cache_metrics(

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """

    Get detailed cache metrics and monitoring data.

    

    Returns current cache metrics, historical data, and alerts.

    """
    try:
        # Collect current metrics
        current_metrics = await cache_monitor.collect_metrics()
        
        # Get performance summary
        performance_summary = cache_monitor.get_performance_summary()
        
        # Get recent alerts
        recent_alerts = cache_monitor.get_alerts(hours=24, limit=10)
        
        return {
            "current_metrics": {
                "timestamp": current_metrics.timestamp.isoformat(),
                "hits": current_metrics.hits,
                "misses": current_metrics.misses,
                "hit_rate": current_metrics.hit_rate,
                "memory_usage": current_metrics.memory_usage,
                "key_count": current_metrics.key_count,
                "avg_ttl": current_metrics.avg_ttl
            },
            "performance_summary": performance_summary,
            "recent_alerts": [
                {
                    "type": alert.alert_type,
                    "severity": alert.severity,
                    "message": alert.message,
                    "timestamp": alert.timestamp.isoformat()
                }
                for alert in recent_alerts
            ]
        }
        
    except Exception as e:
        logger.error(f"Failed to get cache metrics: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to get cache metrics: {str(e)}"
        )


@router.post("/cache/invalidate")
async def invalidate_cache(

    pattern: Optional[str] = None,

    user_id: Optional[str] = None,

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """

    Invalidate cache entries based on pattern or user.

    

    Args:

        pattern: Cache key pattern to invalidate

        user_id: User ID to invalidate cache for

    """
    try:
        deleted_count = 0
        
        if user_id:
            from ...core.cache import CacheInvalidationManager
            deleted_count = await CacheInvalidationManager.invalidate_user_related_cache(user_id)
        elif pattern:
            deleted_count = await cache_manager.delete_pattern(pattern)
        else:
            # Invalidate system cache
            from ...core.cache import CacheInvalidationManager
            deleted_count = await CacheInvalidationManager.invalidate_system_cache()
        
        logger.info(f"Cache invalidation completed: {deleted_count} keys deleted")
        
        return {
            "message": "Cache invalidation completed",
            "deleted_keys": deleted_count,
            "timestamp": datetime.utcnow().isoformat()
        }
        
    except Exception as e:
        logger.error(f"Cache invalidation failed: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Cache invalidation failed: {str(e)}"
        )


@router.post("/cache/warm")
async def warm_cache(

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """

    Warm cache with commonly accessed data.

    

    Preloads frequently accessed endpoints and queries into cache.

    """
    try:
        from ...core.cache import warm_common_queries
        result = await warm_common_queries()
        
        logger.info("Cache warming completed", result=result)
        
        return {
            "message": "Cache warming completed",
            "result": result,
            "timestamp": datetime.utcnow().isoformat()
        }
        
    except Exception as e:
        logger.error(f"Cache warming failed: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Cache warming failed: {str(e)}"
        )


@router.get("/cache/report")
async def get_cache_report(

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """

    Generate comprehensive cache performance report.

    

    Returns detailed analysis of cache performance, recommendations,

    and historical trends.

    """
    try:
        report = await generate_cache_report()
        
        logger.info("Cache report generated")
        
        return report
        
    except Exception as e:
        logger.error(f"Failed to generate cache report: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to generate cache report: {str(e)}"
        )
# Performance monitoring endpoints

@router.get("/performance/summary")
async def get_performance_summary(

    hours: int = 1,

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """

    Get performance summary for the specified time period.

    

    Args:

        hours: Number of hours to analyze (default: 1)

    """
    try:
        from ...core.performance import request_deduplicator, response_cache, connection_optimizer
        from ...middleware.performance import PerformanceMiddleware
        
        # Get performance middleware instance from app state
        # This is a simplified approach - in production you'd want proper instance management
        performance_summary = {
            "deduplication_stats": request_deduplicator.get_stats(),
            "response_cache_stats": response_cache.get_stats(),
            "connection_pool_stats": await connection_optimizer.monitor_redis_pool(),
            "timestamp": datetime.utcnow().isoformat()
        }
        
        return performance_summary
        
    except Exception as e:
        logger.error(f"Failed to get performance summary: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to get performance summary: {str(e)}"
        )


@router.get("/performance/deduplication")
async def get_deduplication_stats(

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """Get request deduplication statistics."""
    try:
        from ...core.performance import request_deduplicator
        
        stats = request_deduplicator.get_stats()
        
        return {
            "deduplication_stats": stats,
            "recommendations": [
                "Enable deduplication for expensive read operations",
                "Monitor deduplication rate to identify optimization opportunities"
            ] if stats["deduplication_rate"] < 10 else [
                "Deduplication is working effectively"
            ]
        }
        
    except Exception as e:
        logger.error(f"Failed to get deduplication stats: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to get deduplication stats: {str(e)}"
        )


@router.get("/performance/connections")
async def get_connection_stats(

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """Get connection pool performance statistics."""
    try:
        from ...core.performance import connection_optimizer
        
        redis_stats = await connection_optimizer.monitor_redis_pool()
        pool_history = connection_optimizer.get_pool_history(hours=1)
        
        return {
            "current_stats": redis_stats,
            "history": pool_history,
            "optimization_tips": [
                "Monitor pool utilization to optimize connection limits",
                "Consider connection pooling for database operations",
                "Use connection health checks to maintain pool quality"
            ]
        }
        
    except Exception as e:
        logger.error(f"Failed to get connection stats: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to get connection stats: {str(e)}"
        )


@router.get("/performance/async")
async def get_async_stats(

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """Get async processing performance statistics."""
    try:
        from ...core.performance import async_optimizer
        
        stats = async_optimizer.get_stats()
        
        return {
            "async_stats": stats,
            "optimization_recommendations": [
                "Use semaphores to limit concurrent operations",
                "Implement batch processing for bulk operations",
                "Set appropriate timeouts for async operations",
                "Monitor semaphore usage to prevent bottlenecks"
            ]
        }
        
    except Exception as e:
        logger.error(f"Failed to get async stats: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Failed to get async stats: {str(e)}"
        )


@router.post("/performance/optimize")
async def optimize_performance(

    current_user: Optional[Dict[str, Any]] = Depends(get_optional_user)

) -> Dict[str, Any]:
    """

    Trigger performance optimization tasks.

    

    This endpoint can be used to manually trigger optimization tasks

    like cache warming, connection pool adjustment, etc.

    """
    try:
        optimization_results = []
        
        # Warm cache
        from ...core.cache import warm_common_queries
        cache_result = await warm_common_queries()
        optimization_results.append({
            "task": "cache_warming",
            "result": cache_result
        })
        
        # Monitor connection pools
        from ...core.performance import connection_optimizer
        pool_result = await connection_optimizer.monitor_redis_pool()
        optimization_results.append({
            "task": "connection_monitoring",
            "result": pool_result
        })
        
        # Collect cache metrics
        from ...core.cache_monitoring import cache_monitor
        metrics_result = await cache_monitor.collect_metrics()
        optimization_results.append({
            "task": "metrics_collection",
            "result": "completed"
        })
        
        return {
            "message": "Performance optimization completed",
            "results": optimization_results,
            "timestamp": datetime.utcnow().isoformat()
        }
        
    except Exception as e:
        logger.error(f"Performance optimization failed: {e}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Performance optimization failed: {str(e)}"
        )