<?php

declare(strict_types=1);

/**
 * APO-196 Card 1 — Site Settings action service.
 *
 * One verb: save-site-settings. Wraps MyronSiteSettingsRepository::saveSettings
 * and refreshes the settings cache. The favicon upload is optional — admin
 * passes the $_FILES array directly; API can pass a JSON-content envelope
 * decoded into a $_FILES-shaped array via MyronJsonContentEnvelope.
 */
final class MyronSiteSettingsActionService
{
    public function __construct(
        private readonly MyronSiteSettingsRepository $repository,
    ) {
    }

    /**
     * @param array<string, mixed> $settings
     * @param array<string, mixed>|null $faviconFile $_FILES-shaped or decoded envelope
     * @return array{updated: bool}
     */
    public function saveSettings(array $settings, ?array $faviconFile, MyronActionCallerContext $caller): array
    {
        $userId = $caller->callerType === 'human-session' ? $caller->callerId : '';
        $this->repository->saveSettings($settings, $faviconFile, $userId);
        if (function_exists('myronRefreshSiteSettingsCache')) {
            myronRefreshSiteSettingsCache();
        }
        return ['updated' => true];
    }
}
