<?php

declare(strict_types=1);

/**
 * APO-193 Card 7 — Delete a saved section variant.
 *
 * URI:   DELETE /api/v1/design/section-variants/{variantId}
 * Scope: design.write
 */
final class MyronDeleteSectionVariantAction implements MyronAction
{
    public function __construct(
        private readonly MyronSectionVariantActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/section-variants/{variantId}',
            method:        'DELETE',
            domain:        'design',
            resource:      'section-variant',
            verb:          'delete',
            inputSchema:   [
                'type'       => 'object',
                'properties' => ['variantId' => ['type' => 'string']],
                'required'   => ['variantId'],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'variantId' => ['type' => 'string'],
                    'deleted'   => ['type' => 'boolean'],
                ],
            ],
            requiredScope:   MyronApiScopes::DESIGN_WRITE,
            summary:         'Delete a saved section variant.',
            sideEffects:     ['deletes from section_variants table'],
            preconditions:   ['variant exists'],
            postconditions:  ['variant row removed'],
            audienceClass:   4,
            adminAliases:    ['delete-variant'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

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