<?php

declare(strict_types=1);

/** APO-195 Card 3 — PATCH /api/v1/content/categories/{categoryId} */
final class MyronUpdateCategoryAction implements MyronAction
{
    public function __construct(private readonly MyronCategoryActionService $service) {}
    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/categories/{categoryId}', method: 'PATCH',
            domain: 'content', resource: 'category', verb: 'update',
            inputSchema: [
                'type' => 'object',
                'properties' => ['categoryId' => ['type' => 'string'], 'title' => ['type' => 'string'], 'slug' => ['type' => 'string'], 'description' => ['type' => 'string'], 'isActive' => ['type' => 'boolean']],
                'required' => ['categoryId'],
            ],
            outputSchema: ['type' => 'object', 'properties' => ['categoryId' => ['type' => 'string']]],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Update a category (title, slug, description, active flag).',
            sideEffects: ['updates page_categories row'], audienceClass: 4,
            adminAliases: ['update-category'], snapshotInclude: false, exposeToMcp: true,
        );
    }
    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            return MyronActionResult::ok($this->service->update(
                (string) ($input['categoryId'] ?? ''),
                (string) ($input['title'] ?? ''),
                (string) ($input['slug'] ?? ''),
                (string) ($input['description'] ?? ''),
                (bool) ($input['isActive'] ?? true),
                $caller
            ));
        } catch (InvalidArgumentException $e) { return MyronActionResult::error('input_invalid', $e->getMessage()); }
        catch (Throwable $e) { return MyronActionResult::error('business_rule_failed', $e->getMessage()); }
    }
}
