<?php

namespace QUI\InfoPopup\MCP\Popup;

use Mcp\Schema\Result\CallToolResult;
use Mcp\Server\Builder;
use QUI\AI\MCP\ToolHelper;
use QUI\InfoPopup\MCP\AbstractTool;
use Throwable;

class ListPopups extends AbstractTool
{
    public function register(Builder $serverBuilder): void
    {
        $serverBuilder->addTool(
            function (
                string $project,
                ?string $search = null,
                ?bool $active = null,
                ?int $limit = null,
                ?int $offset = null
            ): CallToolResult | array {
                try {
                    self::checkPermission();

                    return self::getPopupService()->getList($project, $search, $active, $limit, $offset);
                } catch (Throwable $Exception) {
                    return ToolHelper::parseExceptionToResult($Exception);
                }
            },
            name: 'quiqqer_info_popups_list',
            description: 'Lists project-wide info popups with optional title/content search, active-state filtering and '
                . 'pagination. Use before creating a popup to avoid duplicates.',
            inputSchema: [
                'type' => 'object',
                'additionalProperties' => false,
                'required' => ['project'],
                'properties' => [
                    'project' => ['type' => 'string', 'description' => 'Project name.'],
                    'search' => ['type' => 'string', 'description' => 'Optional title or content search term.'],
                    'active' => ['type' => 'boolean', 'description' => 'Optional active-state filter.'],
                    'limit' => ['type' => 'integer', 'default' => 50, 'minimum' => 1, 'maximum' => 100],
                    'offset' => ['type' => 'integer', 'default' => 0, 'minimum' => 0]
                ]
            ]
        );
    }
}
