<?php

namespace QUI\InfoPopup\MCP;

use QUI\AI\MCP\Server;
use QUI\InfoPopup\Api\PopupService;
use QUI\MCP\ToolInterface;
use QUI\Permissions\Permission;

abstract class AbstractTool implements ToolInterface
{
    public const INFO_POPUP_MCP_PERMISSION = 'quiqqer.infoPopup.mcp';

    protected static function checkPermission(): void
    {
        Permission::checkPermission(
            self::INFO_POPUP_MCP_PERMISSION,
            Server::getRequestUser()
        );
    }

    protected static function getPopupService(): PopupService
    {
        return new PopupService();
    }

    /**
     * @return array<string, mixed>
     */
    protected static function getPopupProperties(): array
    {
        $multilingualValue = [
            'oneOf' => [
                ['type' => 'string'],
                [
                    'type' => 'object',
                    'additionalProperties' => ['type' => 'string']
                ]
            ]
        ];

        return [
            'active' => [
                'type' => 'boolean',
                'description' => 'Whether the popup is published. New popups are inactive by default.'
            ],
            'priority' => ['type' => 'integer', 'description' => 'Higher values are displayed first.'],
            'title' => $multilingualValue + [
                'description' => 'Title as a string or, preferably, an object keyed by language code.'
            ],
            'content' => $multilingualValue + [
                'description' => 'Semantic HTML content as a string or an object keyed by language code.'
            ],
            'languages' => [
                'type' => 'array',
                'items' => ['type' => 'string'],
                'description' => 'Target project language codes. Omit to show the popup in all project languages, '
                    . 'now and in the future; an empty array means the same. A subset restricts the popup to '
                    . 'exactly those languages. Codes outside the project languages are ignored.'
            ],
            'validFrom' => [
                'type' => 'string',
                'description' => 'Optional publication start in YYYY-MM-DD HH:MM:SS format. Empty clears it.'
            ],
            'validTo' => [
                'type' => 'string',
                'description' => 'Optional publication end in YYYY-MM-DD HH:MM:SS format. Empty clears it.'
            ],
            'visibility' => [
                'type' => 'string',
                'enum' => ['always', 'guest', 'authenticated', 'groups'],
                'description' => 'Audience: always for everyone, guest for signed-out visitors, authenticated for '
                    . 'signed-in users, or groups for selected user groups.'
            ],
            'visibilityGroupIds' => [
                'type' => 'array',
                'items' => ['type' => 'integer', 'minimum' => 1],
                'description' => 'Positive user group IDs used only when visibility is groups.'
            ],
            'displayMode' => [
                'type' => 'string',
                'enum' => ['', 'content', 'sidebar', 'sidebarRight', 'top'],
                'description' => 'Empty uses the project default. content has no media area; sidebar, sidebarRight and '
                    . 'top support mediaImage and mediaColor.'
            ],
            'verticalAlign' => [
                'type' => 'string',
                'enum' => ['top', 'center', 'bottom'],
                'description' => 'Vertical alignment of the popup content.'
            ],
            'centerContent' => [
                'type' => 'boolean',
                'description' => 'Horizontally centers the title and unaligned content. Explicit editor alignment '
                    . 'on individual content elements is preserved.'
            ],
            'mediaImage' => ['type' => 'string', 'description' => 'QUIQQER media image reference.'],
            'mediaColor' => ['type' => 'string', 'description' => 'Media area background colour.'],
            'mediaAutoHeight' => [
                'type' => 'boolean',
                'description' => 'Shows the complete image at its natural aspect ratio only when displayMode is top. '
                    . 'Use for promotional banners with embedded sale text, logos, or other content that must not be cropped.'
            ],
            'maxWidth' => ['type' => 'integer', 'minimum' => 0, 'description' => 'Maximum width in pixels; 0 uses the project default.'],
            'maxHeight' => ['type' => 'integer', 'minimum' => 0, 'description' => 'Maximum height in pixels; 0 uses the project default.']
        ];
    }
}
