<?php

// SPDX-FileCopyrightText: 2021 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later

namespace Icinga\Module\Icingadb\Hook;

use Icinga\Application\Icinga;
use Icinga\Authentication\Auth;
use Icinga\Module\Icingadb\Hook\Common\HookUtils;
use Icinga\Web\Session;

abstract class IcingadbSupportHook
{
    use HookUtils;

    /**
     * Return whether your module supports IcingaDB or not
     *
     * @return bool
     */
    public function supportsIcingaDb(): bool
    {
        return true;
    }

    /**
     * Whether icingadb is set as the preferred backend in preferences
     *
     * @return bool Return true if icingadb is set as backend, false otherwise
     */
    final public static function isIcingaDbSetAsPreferredBackend(): bool
    {
        return true;
    }

    /**
     * Whether to use icingadb as the backend
     *
     * @return bool Returns true if monitoring module is accessible or icingadb is selected as backend, false otherwise.
     */
    final public static function useIcingaDbAsBackend(): bool
    {
        return ! Icinga::app()->getModuleManager()->hasEnabled('monitoring')
            || ! Auth::getInstance()->hasPermission('module/monitoring')
            || self::isIcingaDbSetAsPreferredBackend();
    }
}
