<?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 CreatePopup extends AbstractTool
{
    public function register(Builder $serverBuilder): void
    {
        $serverBuilder->addTool(
            function (
                string $project,
                array | string $title,
                array | string | null $content = null,
                ?array $languages = null,
                ?bool $active = null,
                ?int $priority = null,
                ?string $validFrom = null,
                ?string $validTo = null,
                ?string $visibility = null,
                ?array $visibilityGroupIds = null,
                ?string $displayMode = null,
                ?string $verticalAlign = null,
                ?bool $centerContent = null,
                ?string $mediaImage = null,
                ?string $mediaColor = null,
                ?bool $mediaAutoHeight = null,
                ?int $maxWidth = null,
                ?int $maxHeight = null
            ): CallToolResult | array {
                try {
                    self::checkPermission();
                    $attributes = ['title' => $title];

                    foreach (
                        compact(
                            'content',
                            'languages',
                            'active',
                            'priority',
                            'validFrom',
                            'validTo',
                            'visibility',
                            'visibilityGroupIds',
                            'displayMode',
                            'verticalAlign',
                            'centerContent',
                            'mediaImage',
                            'mediaColor',
                            'mediaAutoHeight',
                            'maxWidth',
                            'maxHeight'
                        ) as $field => $value
                    ) {
                        if ($value !== null) {
                            $attributes[$field] = $value;
                        }
                    }

                    return self::getPopupService()->create($project, $attributes);
                } catch (Throwable $Exception) {
                    return ToolHelper::parseExceptionToResult($Exception);
                }
            },
            name: 'quiqqer_info_popups_create',
            description: 'Creates a project-wide multilingual info popup with optional schedule, audience and design '
                . 'settings. New popups are inactive unless active is true.',
            inputSchema: [
                'type' => 'object',
                'additionalProperties' => false,
                'required' => ['project', 'title'],
                'properties' => ['project' => ['type' => 'string', 'description' => 'Project name.']]
                    + self::getPopupProperties()
            ]
        );
    }
}
