<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\src\Mcp\Tools;

use Closure;
use Plugin\tnt_mcp\src\Mcp\Commerce\PaymentMethodReader;
use Plugin\tnt_mcp\src\Mcp\Contracts\Tool;
use Plugin\tnt_mcp\src\Mcp\Mode\EndpointClass;

/**
 * READ tool: the payment methods, one compact row each.
 *
 * `active` and `usable` are two different things and both are reported: `nActive` is configured,
 * `nNutzbar` is derived from what PHP on this server can actually do
 * (`Helpers/PaymentMethod.php:246-257`). A method that is active and not usable is invisible in
 * checkout, and that is the single most common "why can nobody pay" answer.
 */
final class ListPaymentMethods implements Tool
{
    /** @param Closure(): PaymentMethodReader $readerFactory must not touch the DB until run() */
    public function __construct(private readonly Closure $readerFactory)
    {
    }

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

    public function description(): string
    {
        return 'List payment methods with module id, whether each is active and actually usable, '
            . 'sort order and customer-group limits.';
    }

    /** @return array<string,mixed> */
    public function inputSchema(): array
    {
        return [
            'type'       => 'object',
            'properties' => [
                'all' => [
                    'type'        => 'boolean',
                    'description' => 'Ignored. This tool always returns every payment method.',
                ],
            ],
            'additionalProperties' => false,
        ];
    }

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

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

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

    /**
     * @param array<string,mixed> $args
     * @return array<string,mixed>
     */
    public function run(array $args): array
    {
        return ($this->readerFactory)()->methods();
    }
}
