<?php

declare(strict_types=1);

/**
 * APO-193 Card 4 — Activate a footer variant.
 *
 * URI:   POST /api/v1/design/footers/{variantId}/activate
 * Scope: design.write
 */
final class MyronActivateFooterAction implements MyronAction
{
    public function __construct(
        private readonly MyronFooterDesignActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/footers/{variantId}/activate',
            method:        'POST',
            domain:        'design',
            resource:      'footer',
            verb:          'activate',
            inputSchema:   [
                'type'       => 'object',
                'properties' => ['variantId' => ['type' => 'string']],
                'required'   => ['variantId'],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'variantId'   => ['type' => 'string'],
                    'isActivated' => ['type' => 'boolean'],
                ],
            ],
            requiredScope:   MyronApiScopes::DESIGN_WRITE,
            summary:         'Activate a footer variant so it can be selected as default or used in template chrome.',
            sideEffects:     ['writes footer_designs.is_activated'],
            preconditions:   ['variant exists in valid variants'],
            postconditions:  ['footer_designs row for variant has is_activated=1'],
            audienceClass:   4,
            adminAliases:    ['activate-footer-design'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok(
                $this->service->activate((string) ($input['variantId'] ?? ''), $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());
        }
    }
}
