<?php

namespace QUI\InfoPopup\MCP;

use Mcp\Server\Builder;
use QUI\AI\MCP\ProviderInterface;
use QUI\AI\MCP\Server;
use QUI\InfoPopup\MCP\Popup\CreatePopup;
use QUI\InfoPopup\MCP\Popup\DeletePopup;
use QUI\InfoPopup\MCP\Popup\GetPopup;
use QUI\InfoPopup\MCP\Popup\ListPopups;
use QUI\InfoPopup\MCP\Popup\UpdatePopup;
use QUI\MCP\ToolInterface;
use QUI\Permissions\Permission;
use Throwable;

class Provider implements ProviderInterface
{
    /**
     * @var array<ToolInterface>
     */
    protected array $tools;

    public function __construct()
    {
        $this->tools = [
            new ListPopups(),
            new GetPopup(),
            new CreatePopup(),
            new UpdatePopup(),
            new DeletePopup()
        ];
    }

    public function register(Builder $serverBuilder): void
    {
        if (!$this->canUseMcp()) {
            return;
        }

        foreach ($this->tools as $Tool) {
            $Tool->register($serverBuilder);
        }
    }

    protected function canUseMcp(): bool
    {
        try {
            Permission::checkPermission(
                AbstractTool::INFO_POPUP_MCP_PERMISSION,
                Server::getRequestUser()
            );

            return true;
        } catch (Throwable) {
            return false;
        }
    }
}
