<?php

declare(strict_types=1);

final class MyronSaveCommentSettingsAction implements MyronAction
{
    public function __construct(private readonly BlogCommentService $service)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/comments/settings',
            method: 'PATCH',
            domain: 'content',
            resource: 'comment-settings',
            verb: 'save',
            inputSchema: ['type' => 'object', 'properties' => ['notification_email_id' => ['type' => 'string']]],
            outputSchema: ['type' => 'object'],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Save Blog comment moderation notification routing settings.',
            sideEffects: ['updates blog_comment_settings'],
            preconditions: ['notification destination is verified when provided'],
            postconditions: ['comment notification routing reflects input'],
            audienceClass: 4,
            adminAliases: ['save-comment-settings'],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

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