<?php
namespace EasyChat\Library\Messenger;

class MaxPersonalSettingsService {

    private $lifecycle;
    private $incidents;

    public function __construct($registry) {
        $this->lifecycle = new MaxSessionLifecycle($registry);
        $this->incidents = new MaxPersonalIncidentService($registry);
    }

    public function save(array $channel, array $input): array {
        $scope = MaxInboundScope::fromPost($input['scope'] ?? []);
        if (!$scope) {
            throw new \InvalidArgumentException('Выберите хотя бы один источник сообщений.');
        }
        $expected_revision = max(0, (int)($input['revision'] ?? 0));
        $status = !empty($input['enabled']) ? 'active' : 'disabled';
        $context = $this->lifecycle->saveConfiguration(
            $channel,
            $status,
            [],
            [
                'dedicated_cron_mode' => MaxPollingService::normalizeDedicatedCronMode((string)($input['dedicated_cron_mode'] ?? '')),
                'personal_inbound_scope' => $scope,
            ],
            false,
            $expected_revision
        );
        if ($status === 'disabled') {
             
             
            $this->incidents->resolve((int)($channel['channel_id'] ?? 0));
        }
        return $context;
    }
}
