<?php

declare(strict_types=1);

/** APO-196 Card 3 — POST /api/v1/administration/updates/apply-remote (exposeToMcp:true, APO-205-007). */
final class MyronApplyRemoteApplicationUpdateAction implements MyronAction
{
    public function __construct(private readonly MyronSiteSetupActionService $service) {}
    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/administration/updates/apply-remote', method: 'POST',
            domain: 'administration', resource: 'application-update', verb: 'apply-remote',
            inputSchema: ['type' => 'object', 'properties' => []],
            outputSchema: ['type' => 'object', 'properties' => []],
            requiredScope: MyronApiScopes::ADMINISTRATION_DESTRUCTIVE,
            summary: 'Apply the available remote application update (pulls the published manifest, downloads the package, applies). Mutates install files; the current PHP process may restart mid-apply.',
            sideEffects: ['downloads + applies remote update package'],
            preconditions: ['caller is administrator', 'remote update is available'],
            postconditions: ['remote package applied'],
            audienceClass: 4, adminAliases: ['apply-remote-application-update'],
            snapshotInclude: false, exposeToMcp: true,
        );
    }
    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try { return MyronActionResult::ok($this->service->applyRemoteApplicationUpdate($caller)); }
        catch (MyronActionPreconditionError $e) { return MyronActionResult::error('precondition_failed', $e->getMessage()); }
        catch (Throwable $e) { return MyronActionResult::error('business_rule_failed', $e->getMessage()); }
    }
}
