<?php

namespace OPNsense\RMMAgent\Api;

use OPNsense\Base\ApiControllerBase;

class ServiceController extends ApiControllerBase
{
    public function statusAction()
    {
        $backend = new \OPNsense\Core\Backend();
        $response = trim($backend->configdRun('rmmagent status'));

        if (strpos($response, 'is running') !== false) {
            return array('status' => 'running', 'message' => $response);
        } else {
            return array('status' => 'stopped', 'message' => $response);
        }
    }

    public function startAction()
    {
        $backend = new \OPNsense\Core\Backend();
        $response = trim($backend->configdRun('rmmagent start'));
        return array('status' => 'ok', 'message' => $response);
    }

    public function stopAction()
    {
        $backend = new \OPNsense\Core\Backend();
        $response = trim($backend->configdRun('rmmagent stop'));
        return array('status' => 'ok', 'message' => $response);
    }

    public function restartAction()
    {
        $backend = new \OPNsense\Core\Backend();
        $response = trim($backend->configdRun('rmmagent restart'));
        return array('status' => 'ok', 'message' => $response);
    }

    public function reconfigureAction()
    {
        $backend = new \OPNsense\Core\Backend();
        $response = trim($backend->configdRun('rmmagent reconfigure'));
        return array('status' => 'ok', 'message' => $response);
    }
}
