<?php

declare(strict_types=1);

/**
 * APO-231 — Delete an unused operator-owned Custom Section Template.
 *
 * URI:   DELETE /api/v1/design/section-templates/custom/{sectionKey}
 * Scope: design.write
 */
final class MyronDeleteCustomSectionTemplateAction implements MyronAction
{
    public function __construct(
        private readonly MyronCustomSectionTemplateActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/section-templates/custom/{sectionKey}',
            method:        'DELETE',
            domain:        'design',
            resource:      'section-template',
            verb:          'delete-custom',
            inputSchema:   [
                'type' => 'object',
                'properties' => ['sectionKey' => ['type' => 'string']],
                'required' => ['sectionKey'],
            ],
            outputSchema:  ['type' => 'object', 'properties' => ['deleted' => ['type' => 'boolean']]],
            requiredScope: MyronApiScopes::DESIGN_WRITE,
            summary:       'Delete an unused Custom Section Template.',
            sideEffects:   ['deletes custom_section_templates row'],
            preconditions: ['custom section template is not used by templates/pages or saved variants'],
            postconditions:['custom section template is removed'],
            audienceClass: 4,
            adminAliases:  ['delete-custom-section-template'],
            snapshotInclude: false,
            exposeToMcp:   true,
        );
    }

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