<?php

declare(strict_types=1);

/**
 * APO-194 Card 5 — Reset a template's chrome assignment (fall back to global default).
 *
 * URI:   DELETE /api/v1/design/templates/{templateId}/chrome
 * Scope: design.write
 */
final class MyronResetTemplateChromeAction implements MyronAction
{
    public function __construct(
        private readonly MyronTemplateChromeActionService $service,
    ) {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri:           '/api/v1/design/templates/{templateId}/chrome',
            method:        'DELETE',
            domain:        'design',
            resource:      'template-chrome',
            verb:          'reset',
            inputSchema:   [
                'type'       => 'object',
                'properties' => ['templateId' => ['type' => 'string']],
                'required'   => ['templateId'],
            ],
            outputSchema:  [
                'type'       => 'object',
                'properties' => [
                    'templateId' => ['type' => 'string'],
                    'reset'      => ['type' => 'boolean'],
                ],
            ],
            requiredScope:   MyronApiScopes::DESIGN_WRITE,
            summary:         'Clear a template chrome assignment so the template renders the global default navigation + footer.',
            sideEffects:     ['deletes from template_chrome_assignments'],
            preconditions:   ['template exists'],
            postconditions:  ['no template_chrome_assignments row for the template; renders inherit:global'],
            audienceClass:   4,
            adminAliases:    [],
            snapshotInclude: false,
            exposeToMcp:     true,
        );
    }

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