<?php

/**
 *
 * bareos-webui - Bareos Web-Frontend
 *
 * @link      https://github.com/bareos/bareos for the canonical source repository
 * @copyright Copyright (C) 2013-2026 Bareos GmbH & Co. KG (http://www.bareos.org/)
 * @license   GNU Affero General Public License (http://www.gnu.org/licenses/)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace Pool\Controller;

use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Exception;
use Throwable;

class PoolController extends AbstractActionController
{
    /**
     * Variables
     */
    protected $poolModel = null;
    protected $bsock = null;
    protected $acl_alert = false;

    /**
     * Get Index Action
     *
     * @return object
     */
    public function indexAction()
    {
        $this->RequestURIPlugin()->setRequestURI();

        if (!$this->SessionTimeoutPlugin()->isValid()) {
            return $this->redirect()->toRoute(
                'auth',
                array(
                    'action' => 'login'
                ),
                array(
                    'query' => array(
                        'req' => $this->RequestURIPlugin()->getRequestURI(),
                        'dird' => $_SESSION['bareos']['director']
                    )
                )
            );
        }

        $module_config = $this->getServiceLocator()->get('ModuleManager')->getModule('Application')->getConfig();
        $invalid_commands = $this->CommandACLPlugin()->getInvalidCommands(
            $module_config['console_commands']['Pool']['mandatory']
        );
        if (count($invalid_commands) > 0) {
            $this->acl_alert = true;
            return new ViewModel(
                array(
                    'acl_alert' => $this->acl_alert,
                    'invalid_commands' => implode(",", $invalid_commands)
                )
            );
        }

        $pools = array();

        try {
            $this->bsock = $this->getServiceLocator()->get('director');
            $pools = $this->getPoolModel()->getPools($this->bsock);
        } catch (Throwable $e) {
            error_log($e->getMessage());
            return new ViewModel(
                array(
                    'pools' => $pools,
                    'error' => 'Failed to load pools',
                )
            );
        } finally {
            if ($this->bsock) {
                try {
                    $this->bsock->disconnect();
                } catch (Throwable $e) {
                    error_log($e->getMessage());
                }
            }
        }

        return new ViewModel(
            array(
                'pools' => $pools,
            )
        );
    }

    /**
     * Get Details Action
     *
     * @return object
     */
    public function detailsAction()
    {
        $this->RequestURIPlugin()->setRequestURI();

        if (!$this->SessionTimeoutPlugin()->isValid()) {
            return $this->redirect()->toRoute(
                'auth',
                array(
                    'action' => 'login'
                ),
                array(
                    'query' => array(
                        'req' => $this->RequestURIPlugin()->getRequestURI(),
                        'dird' => $_SESSION['bareos']['director']
                    )
                )
            );
        }

        $module_config = $this->getServiceLocator()->get('ModuleManager')->getModule('Application')->getConfig();
        $invalid_commands = $this->CommandACLPlugin()->getInvalidCommands(
            $module_config['console_commands']['Pool']['mandatory']
        );
        if (count($invalid_commands) > 0) {
            $this->acl_alert = true;
            return new ViewModel(
                array(
                    'acl_alert' => $this->acl_alert,
                    'invalid_commands' => implode(",", $invalid_commands)
                )
            );
        }

        $poolname = $this->params()->fromQuery('pool');

        try {
            $this->bsock = $this->getServiceLocator()->get('director');
            $this->getPoolModel()->getPool($this->bsock, $poolname);
        } catch (Throwable $e) {
            error_log($e->getMessage());
            return new ViewModel(
                array(
                    'pool' => $poolname,
                    'error' => 'Failed to load pool',
                )
            );
        } finally {
            if ($this->bsock) {
                try {
                    $this->bsock->disconnect();
                } catch (Throwable $e) {
                    error_log($e->getMessage());
                }
            }
        }

        return new ViewModel(
            array(
                'pool' => $poolname,
            )
        );
    }

    /**
     * Get Pool Model
     *
     * @return object
     */
    public function getPoolModel()
    {
        if (!$this->poolModel) {
            $sm = $this->getServiceLocator();
            $this->poolModel = $sm->get('Pool\Model\PoolModel');
        }
        return $this->poolModel;
    }
}
