<?php

declare(strict_types=1);

/**
 * APO-195 Card 1 — Delete a page.
 *
 * URI:   DELETE /api/v1/content/pages/{pageId}
 * Scope: content.write
 */
final class MyronDeletePageAction implements MyronAction
{
    public function __construct(private readonly MyronPagesActionService $service) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/content/pages/{pageId}', method: 'DELETE',
            domain: 'content', resource: 'page', verb: 'delete',
            inputSchema: ['type' => 'object', 'properties' => ['pageId' => ['type' => 'string']], 'required' => ['pageId']],
            outputSchema: ['type' => 'object', 'properties' => ['pageId' => ['type' => 'string'], 'deleted' => ['type' => 'boolean']]],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Delete a page (cascades to page_versions; removes path from public surface).',
            sideEffects: ['deletes pages row + page_versions rows'],
            preconditions: ['page exists; deletion policy permits'],
            postconditions: ['page removed; path no longer routable'],
            audienceClass: 4,
            adminAliases: ['delete-page'],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

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