<?php

declare(strict_types=1);

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