<?php

declare(strict_types=1);

final class MyronDeleteRedirectAction implements MyronAction
{
    public function __construct(private readonly DisabledPageRedirectsRepository $repository) {}

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/administration/redirects/{slug}',
            method: 'DELETE',
            domain: 'administration',
            resource: 'redirect',
            verb: 'delete',
            inputSchema: ['type' => 'object', 'properties' => ['slug' => ['type' => 'string']], 'required' => ['slug']],
            outputSchema: ['type' => 'object', 'properties' => ['deleted' => ['type' => 'boolean'], 'slug' => ['type' => 'string']]],
            requiredScope: MyronApiScopes::ADMINISTRATION_WRITE,
            summary: 'Delete a disabled-page redirect row.',
            sideEffects: ['deletes disabled_page_redirects row'],
            preconditions: ['redirect exists'],
            postconditions: ['redirect row removed'],
            audienceClass: 4,
            adminAliases: ['redirects.delete'],
            snapshotInclude: false,
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        try {
            $slug = myronNormalizePath('/' . rawurldecode(trim((string) ($input['slug'] ?? ''))));
            if (!$this->repository->exists($slug)) {
                return MyronActionResult::error('not_found', 'Redirect not found.');
            }
            return MyronActionResult::ok(['deleted' => $this->repository->delete($slug), 'slug' => $slug]);
        } catch (Throwable $e) {
            return MyronActionResult::error('business_rule_failed', $e->getMessage());
        }
    }
}
