<?php

declare(strict_types=1);

final class MyronAbortFileUploadSessionAction implements MyronAction
{
    public function __construct(private readonly MyronUploadSessionService $sessions)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/files/upload-sessions/{sessionId}',
            method: 'DELETE',
            domain: 'content',
            resource: 'file-upload-session',
            verb: 'abort',
            inputSchema: [
                'type' => 'object',
                'properties' => [
                    'sessionId' => ['type' => 'string'],
                ],
                'required' => ['sessionId'],
            ],
            outputSchema: ['type' => 'object'],
            requiredScope: MyronApiScopes::CONTENT_WRITE,
            summary: 'Abort and remove a temporary file upload session.',
            sideEffects: ['deletes temporary upload session files'],
            preconditions: ['session exists'],
            postconditions: ['session metadata and temporary bytes are removed'],
            audienceClass: 4,
            adminAliases: [],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

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