<?php

declare(strict_types=1);

/** APO-195 Card 4 — PATCH /api/v1/content/menus/placements */
final class MyronSaveMenuPlacementsAction implements MyronAction
{
    public function __construct(private readonly MyronMenuActionService $service) {}
    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/menus/placements', method: 'PATCH',
            domain: 'content', resource: 'menu-placement', verb: 'save',
            inputSchema: ['type' => 'object', 'properties' => ['placements' => ['type' => 'object']], 'required' => ['placements']],
            outputSchema: ['type' => 'object', 'properties' => ['placementsUpdated' => ['type' => 'integer']]],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Update placement→menu assignments (which menu fills each named placement slot like "primary" or "footer").',
            sideEffects: ['updates menu_placements rows'], audienceClass: 4,
            adminAliases: ['save-placement-assignments'], snapshotInclude: false, exposeToMcp: true,
        );
    }
    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            $placements = is_array($input['placements'] ?? null) ? (array) $input['placements'] : [];
            return MyronActionResult::ok($this->service->savePlacementAssignments($placements, $caller));
        } catch (Throwable $e) { return MyronActionResult::error('business_rule_failed', $e->getMessage()); }
    }
}
