<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Responses\ApiResponse;
use App\Services\ServerInfoService;
use Illuminate\Http\JsonResponse;

class ServerInfoController extends Controller
{
    protected ServerInfoService $serverInfoService;

    public function __construct(ServerInfoService $serverInfoService)
    {
        $this->serverInfoService = $serverInfoService;
    }

    public function index(): JsonResponse
    {
        try {
            $data = [
                'environment' => $this->serverInfoService->getEnvironmentInfo(),
                'system' => $this->serverInfoService->getSystemInfo(),
                'extensions' => $this->serverInfoService->getExtensionCheck(),
            ];

            return response()->json(ApiResponse::success($data));
        } catch (\Throwable $e) {
            return response()->json(ApiResponse::error(50001, '获取服务器信息失败：' . $e->getMessage()));
        }
    }

    /**
     * 检测与中心服务器的通讯状态
     */
    public function checkCommunication(): JsonResponse
    {
        try {
            $result = $this->serverInfoService->checkServerCommunication();

            return response()->json(ApiResponse::success($result));
        } catch (\Throwable $e) {
            return response()->json(ApiResponse::error(50002, '通讯检测失败：' . $e->getMessage()));
        }
    }
}
