<?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 UpdatePopup extends AbstractTool
{
    public function register(Builder $serverBuilder): void
    {
        $serverBuilder->addTool(
            function (
                string $project,
                int $id,
                array | string | null $title = null,
                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 = [];

                    foreach (
                        compact(
                            'title',
                            '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()->update($project, $id, $attributes);
                } catch (Throwable $Exception) {
                    return ToolHelper::parseExceptionToResult($Exception);
                }
            },
            name: 'quiqqer_info_popups_update',
            description: 'Updates only the provided fields of one project-bound info popup; omitted fields remain '
                . 'unchanged. Load the popup first and preserve unrelated language entries.',
            inputSchema: [
                'type' => 'object',
                'additionalProperties' => false,
                'required' => ['project', 'id'],
                'properties' => [
                    'project' => ['type' => 'string', 'description' => 'Project name.'],
                    'id' => ['type' => 'integer', 'description' => 'Info popup ID.', 'minimum' => 1]
                ] + self::getPopupProperties()
            ]
        );
    }
}
