<?php

declare(strict_types=1);

final class MyronBundleImpactPreviewAction implements MyronAction
{
    public function __construct(private readonly MyronBundleImpactAnalysisService $service) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/administration/bundles/{bundleId}/impact-preview',
            method: 'GET',
            domain: 'administration',
            resource: 'bundle',
            verb: 'impactPreview',
            inputSchema: ['type' => 'object', 'properties' => ['bundleId' => ['type' => 'string']], 'required' => ['bundleId']],
            outputSchema: ['type' => 'object', 'properties' => ['bundle_id' => ['type' => 'string']]],
            requiredScope: MyronApiScopes::ADMINISTRATION_READ,
            summary: 'Preview pages, menu items, internal links, and mic references affected by disabling a site type bundle.',
            sideEffects: [],
            preconditions: ['bundle exists'],
            postconditions: ['no state changes are made'],
            audienceClass: 3,
            adminAliases: ['bundles.impactPreview'],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

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