<?php

declare(strict_types=1);

/**
 * APO-196 Card 2 — Send a test mail to the configured admin address.
 *
 * URI:   POST /api/v1/administration/mail/test
 * Scope: administration.write
 *
 * Useful for AI clients to verify setup; exposeToMcp:true (no credential
 * disclosure — just triggers the send).
 */
final class MyronSendTestMailAction implements MyronAction
{
    public function __construct(private readonly MyronMailActionService $service) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/administration/mail/test', method: 'POST',
            domain: 'administration', resource: 'mail-test', verb: 'send',
            inputSchema: ['type' => 'object', 'properties' => []],
            outputSchema: ['type' => 'object', 'properties' => ['recipient' => ['type' => 'string']]],
            requiredScope: MyronApiScopes::ADMINISTRATION_WRITE,
            summary: 'Send a test email to the configured admin address. Useful for verifying SMTP/ESP setup.',
            sideEffects: ['sends one email via configured transport'],
            preconditions: ['mail settings configured'],
            postconditions: ['test mail dispatched'],
            audienceClass: 4, adminAliases: ['send-test-mail'],
            snapshotInclude: false, exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->sendTest($caller));
        } catch (Throwable $e) { return MyronActionResult::error('business_rule_failed', $e->getMessage()); }
    }
}
