<?php

declare(strict_types=1);

namespace PhpWA\Client;

final class DisconnectClassifier
{
    public function stream(int $code, ?string $conflictType = null): DisconnectDecision
    {
        if ($code === 515) { return new DisconnectDecision($code, 'restart_required', true, false); }
        if ($code === 408) { return new DisconnectDecision($code, 'connection_timeout', true, false); }
        if ($code === 503) { return new DisconnectDecision($code, 'service_unavailable', true, false); }
        if ($code === 401 || $code === 403 || $code === 406) {
            return new DisconnectDecision($code, 'logged_out', false, true);
        }
        if ($code === 440 || $conflictType === 'replaced') {
            return new DisconnectDecision($code, 'connection_replaced', false, false);
        }
        if ($code === 500) { return new DisconnectDecision($code, 'bad_session', false, false); }
        return new DisconnectDecision($code, 'stream_error', false, false);
    }

    public function failure(int $code): DisconnectDecision
    {
        if ($code === 500 || $code === 503) {
            return new DisconnectDecision($code, $code === 503 ? 'service_unavailable' : 'server_error', true, false);
        }
        if ($code === 401 || $code === 403 || $code === 406) {
            return new DisconnectDecision($code, 'logged_out', false, true);
        }
        if ($code === 408) { return new DisconnectDecision($code, 'connection_timeout', true, false); }
        return new DisconnectDecision($code, 'connection_failure', false, false);
    }
}
