<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\src\Mcp\Tools;

use Plugin\tnt_mcp\src\Mcp\Contracts\Tool;
use Plugin\tnt_mcp\src\Mcp\Mode\EndpointClass;
use Plugin\tnt_mcp\src\Mcp\Settings\SettingsRegistry;

/** READ tool: discovery layer — lists every setting the MCP knows about, with its mode. */
final class ListSettings implements Tool
{
    public function __construct(private readonly SettingsRegistry $registry)
    {
    }

    public function name(): string
    {
        return 'list_settings';
    }

    public function description(): string
    {
        return 'List every shop setting the MCP server can read or write, with a short title, ' .
            'summary and whether it is read-only, write-only or both.';
    }

    /** @return array<string,mixed> */
    public function inputSchema(): array
    {
        return ['type' => 'object', 'properties' => [], 'additionalProperties' => false];
    }

    public function isWrite(): bool
    {
        return false;
    }

    public function scope(): string
    {
        return 'settings:read';
    }

    public function endpointClass(): EndpointClass
    {
        return EndpointClass::Reversible;
    }

    /**
     * @param array<string,mixed> $args
     * @return array<string,mixed>
     */
    public function run(array $args): array
    {
        $settings = [];
        foreach ($this->registry->all() as $row) {
            $settings[] = [
                'key'     => $row->key,
                'title'   => $row->title,
                'summary' => $row->summary,
                'mode'    => $row->mode,
            ];
        }

        return ['settings' => $settings];
    }
}
