<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\MonitoringService;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class MonitoringController extends Controller
{
    protected MonitoringService $monitoringService;
    
    public function __construct(MonitoringService $monitoringService)
    {
        $this->monitoringService = $monitoringService;
    }
    
    /**
     * Get system metrics and monitoring data
     */
    public function getMetrics(): JsonResponse
    {
        try {
            $metrics = $this->monitoringService->monitorSystemMetrics();
            
            return response()->json([
                'success' => true,
                'data' => $metrics
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to retrieve metrics: ' . $e->getMessage()
            ], 500);
        }
    }
    
    /**
     * Get system health status
     */
    public function getHealth(): JsonResponse
    {
        try {
            $metrics = $this->monitoringService->monitorSystemMetrics();
            $health = $metrics['system_health'] ?? [];
            
            return response()->json([
                'success' => true,
                'data' => [
                    'status' => $health['overall_status'] ?? 'unknown',
                    'health' => $health
                ]
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to retrieve health status: ' . $e->getMessage()
            ], 500);
        }
    }
    
    /**
     * Get market metrics
     */
    public function getMarketMetrics(): JsonResponse
    {
        try {
            $metrics = $this->monitoringService->monitorSystemMetrics();
            
            return response()->json([
                'success' => true,
                'data' => $metrics['market_metrics'] ?? []
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to retrieve market metrics: ' . $e->getMessage()
            ], 500);
        }
    }
}