<?php

declare(strict_types=1);

final class MyronGetTopPagesAction implements MyronAction
{
    public function __construct(private readonly MyronSiteAnalyticsRepository $repo)
    {
    }

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/analytics/top-pages',
            method: 'GET',
            domain: 'analytics',
            resource: 'top-pages',
            verb: 'get',
            inputSchema: ['type' => 'object', 'properties' => ['days' => [], 'limit' => []]],
            outputSchema: ['type' => 'object'],
            requiredScope: MyronApiScopes::ADMINISTRATION_READ,
            summary: 'List top public site pages by pageview count for the selected day range.',
            sideEffects: [],
            preconditions: ['caller holds administration.read'],
            postconditions: ['ranked page paths returned'],
            audienceClass: 2,
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        if (!myronAnalyticsEnabled()) {
            return MyronActionResult::ok(['pages' => []]);
        }
        return MyronActionResult::ok([
            'pages' => $this->repo->getTopPages((int) ($input['days'] ?? 30), (int) ($input['limit'] ?? 10)),
        ]);
    }
}
