<?php

declare(strict_types=1);

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

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/footers/{variantId}/deactivate',
            method:        'POST',
            domain:        'design',
            resource:      'footer',
            verb:          'deactivate',
            inputSchema:   [
                'type'       => 'object',
                'properties' => ['variantId' => ['type' => 'string']],
                'required'   => ['variantId'],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'variantId'   => ['type' => 'string'],
                    'isActivated' => ['type' => 'boolean'],
                ],
            ],
            requiredScope:   MyronApiScopes::DESIGN_WRITE,
            summary:         'Deactivate a footer variant. Cannot deactivate the current global default — set a different default first.',
            sideEffects:     ['writes footer_designs.is_activated'],
            preconditions:   ['variant exists in valid variants', 'variant is not the current global default'],
            postconditions:  ['footer_designs row for variant has is_activated=0'],
            audienceClass:   4,
            adminAliases:    ['deactivate-footer-design'],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok(
                $this->service->deactivate((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());
        }
    }
}
