<?php

declare(strict_types=1);

/** v1.1.04 — POST /api/v1/content/templates/{sourceTemplateId}/sections/remove */
final class MyronRemoveTemplateSectionAction implements MyronAction
{
    public function __construct(private readonly MyronSectionEditActionService $service) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/templates/{sourceTemplateId}/sections/remove',
            method: 'POST',
            domain: 'content',
            resource: 'template-section',
            verb: 'remove',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'sourceTemplateId' => ['type' => 'string'],
                    'position' => ['type' => 'integer', 'minimum' => 0],
                    'sectionUuid' => ['type' => 'string', 'minLength' => 1],
                ],
                'required' => ['sourceTemplateId'],
                'oneOf' => [
                    ['required' => ['position']],
                    ['required' => ['sectionUuid']],
                ],
            ],
            outputSchema: [
                'type' => 'object',
                'properties' => [
                    'removedPosition' => ['type' => 'integer'],
                    'removedSectionUuid' => ['type' => 'string'],
                    'removedInstanceKey' => ['type' => 'string'],
                    'remainingCount' => ['type' => 'integer'],
                ],
            ],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Remove one runtime-template section by position or stable section UUID.',
            sideEffects: ['updates runtime template composition and default content', 'invalidates public page cache'],
            preconditions: ['runtime draft exists', 'exactly one selector is supplied', 'selector identifies one section'],
            postconditions: ['one section and only its template-default content entry are removed', 'page-version content remains available for rollback'],
            audienceClass: 4,
            adminAliases: ['remove-section'],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->removeSection(
                (string) ($input['sourceTemplateId'] ?? ''),
                array_key_exists('position', $input) ? (int) $input['position'] : null,
                array_key_exists('sectionUuid', $input) ? (string) $input['sectionUuid'] : null,
                $caller
            ));
        } catch (MyronActionPreconditionError $e) {
            return MyronActionResult::error('precondition_failed', $e->getMessage());
        } catch (InvalidArgumentException $e) {
            return MyronActionResult::error('input_invalid', $e->getMessage());
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
