<?php
// src/OgBootBundle/Controller/OgBootController.php

namespace App\OgBootBundle\Controller;
require_once __DIR__ . '/../constants.php';
use OpenApi\Annotations as OA;
use Psr\Log\LoggerInterface;
use App\OgBootBundle\Service\CurlRequestService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Exception;


class OgBootController
{
    private $logger;
    /**
     * @OA\Info(title="Ogboot API", version="1.0")
     */

    private $curlRequestService;

    public function __construct(CurlRequestService $curlRequestService, LoggerInterface $logger)
    {
        $this->curlRequestService = $curlRequestService;
        $this->logger = $logger;
    }

/**
 * @OA\Post(
 *     path="/poweroff",
 *     summary="Power off a client",
 *     @OA\RequestBody(
 *         description="JSON payload",
 *         required=true,
 *         @OA\JsonContent(
 *             type="object",
 *             @OA\Property(property="clients", type="array", @OA\Items(type="string", example="192.168.1.1"))
 *         )
 *     ),
 *     @OA\Response(
 *         response=200,
 *         description="Clients powered off successfully",
 *         @OA\JsonContent(
 *             type="object",
 *             @OA\Property(property="success", type="string")
 *         )
 *     ),
 *     @OA\Response(
 *         response=400,
 *         description="Error occurred",
 *         @OA\JsonContent(
 *             type="object",
 *             @OA\Property(property="error", type="string")
 *         )
 *     )
 * )
 * @Route("/poweroff", name="poweroff", methods={"POST"})
 */
    public function poweroff(Request $request): Response
    {
        $data = json_decode($request->getContent(), true);

        if (!isset($data['clients'])) {
            return new Response('Invalid data', Response::HTTP_BAD_REQUEST);
        }

        $ips = $data['clients'];

        $result = $this->curlRequestService->common_request(OG_REST_CMD_POWEROFF, POST, [OG_REST_PARAM_CLIENTS => $ips]);

        if ($result) {
            return new Response('Success', Response::HTTP_OK);
        } else {
            return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR);
        }
    }

/**
 * @OA\Post(
 *     path="/reboot",
 *     summary="Reboot a client",
 *     @OA\RequestBody(
 *         description="JSON payload",
 *         required=true,
 *         @OA\JsonContent(
 *             type="object",
 *             @OA\Property(property="clients", type="array", @OA\Items(type="string", example="192.168.1.1"))
 *         )
 *     ),
 *     @OA\Response(
 *         response=200,
 *         description="Clients rebooted successfully",
 *         @OA\JsonContent(
 *             type="object",
 *             @OA\Property(property="success", type="string")
 *         )
 *     ),
 *     @OA\Response(
 *         response=400,
 *         description="Error occurred",
 *         @OA\JsonContent(
 *             type="object",
 *             @OA\Property(property="error", type="string")
 *         )
 *     )
 * )
 * @Route("/reboot", name="reboot", methods={"POST"})
 */
    public function reboot(Request $request): Response
    {
        $data = json_decode($request->getContent(), true);

        if (!isset($data['clients'])) {
            return new Response('Invalid data', Response::HTTP_BAD_REQUEST);
        }

        $ips = $data['clients'];

        $result = $this->curlRequestService->common_request(OG_REST_CMD_REBOOT, 'POST', [OG_REST_PARAM_CLIENTS => $ips]);

        if ($result) {
            return new Response('Success', Response::HTTP_OK);
        } else {
            return new Response('Failed', Response::HTTP_INTERNAL_SERVER_ERROR);
        }
    }


}
