<?php

declare(strict_types=1);

/**
 * APO-193 Card 8 — Update section defaults.
 *
 * URI:   PATCH /api/v1/design/section-defaults
 * Scope: design.write
 */
final class MyronUpdateSectionDefaultsAction implements MyronAction
{
    public function __construct(
        private readonly MyronSectionDefaultsActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/section-defaults',
            method:        'PATCH',
            domain:        'design',
            resource:      'section-defaults',
            verb:          'update',
            inputSchema:   [
                'type'       => 'object',
                'properties' => [
                    'componentId' => ['type' => 'string'],
                    'variant'     => ['type' => 'string'],
                    'sectionKey'  => ['type' => 'string'],
                    'content'     => ['type' => 'object'],
                    'editability' => ['type' => 'string'],
                ],
                'required'   => ['componentId'],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'componentId' => ['type' => 'string'],
                    'variant'     => ['type' => 'string'],
                    'sectionKey'  => ['type' => 'string'],
                    'scope'       => ['type' => 'string'],
                ],
            ],
            requiredScope:   MyronApiScopes::DESIGN_WRITE,
            summary:         'Update default content + editability for a section template. With sectionKey, scope is per-section-key; without, scope is per-component+variant.',
            sideEffects:     ['writes section_content_defaults OR section_component_defaults depending on scope'],
            preconditions:   ['componentId references a live render component'],
            postconditions:  ['default content + editability persist for the chosen scope'],
            audienceClass:   4,
            adminAliases:    ['update-section-defaults'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            $content = is_array($input['content'] ?? null) ? (array) $input['content'] : [];
            return MyronActionResult::ok($this->service->updateDefaults(
                (string) ($input['componentId'] ?? ''),
                (string) ($input['variant'] ?? ''),
                (string) ($input['sectionKey'] ?? ''),
                $content,
                (string) ($input['editability'] ?? 'inherit'),
                $caller
            ));
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
