<?php

declare(strict_types=1);

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

    public function metadata(): MyronActionMetadata
    {
        return new MyronActionMetadata(
            uri: '/api/v1/analytics/summary',
            method: 'GET',
            domain: 'analytics',
            resource: 'summary',
            verb: 'get',
            inputSchema: ['type' => 'object', 'properties' => ['days' => []]],
            outputSchema: ['type' => 'object'],
            requiredScope: MyronApiScopes::ADMINISTRATION_READ,
            summary: 'Get a privacy-first site analytics summary for the selected day range.',
            sideEffects: [],
            preconditions: ['caller holds administration.read'],
            postconditions: ['summary counts returned without exposing visitor identifiers'],
            audienceClass: 2,
            exposeToMcp: true,
        );
    }

    public function execute(array $input, MyronActionCallerContext $caller): MyronActionResult
    {
        if (!myronAnalyticsEnabled()) {
            return MyronActionResult::ok(['visits' => 0, 'pageviews' => 0, 'top_country' => '', 'top_device' => '', 'top_page' => '', 'top_referrer' => '']);
        }
        return MyronActionResult::ok($this->repo->getSummary((int) ($input['days'] ?? 30)));
    }
}
