<?php

namespace App\Http\Controllers\Api;

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

class DashboardController extends Controller
{
    public function __construct(protected DashboardService $dashboardService) {}

    public function summary(Request $request): JsonResponse
    {
        try {
            $data = $this->dashboardService->getSummary($request->user());

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'خلاصه داشبورد',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت اطلاعات داشبورد',
            ], 500);
        }
    }

    public function charts(): JsonResponse
    {
        try {
            $data = $this->dashboardService->getChartData();

            return response()->json([
                'success' => true,
                'data' => $data,
                'message' => 'نمودارهای داشبورد',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت نمودارها',
            ], 500);
        }
    }

    public function monitoring(Request $request): JsonResponse
    {
        try {
            return response()->json([
                'success' => true,
                'data' => $this->dashboardService->getMonitoring($request->user()),
                'message' => 'مرکز مانیتورینگ',
            ]);
        } catch (\Throwable $e) {
            report($e);

            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت اطلاعات مانیتورینگ',
            ], 500);
        }
    }
}
