<?php

declare(strict_types=1);

namespace App\Framework\Router\Result;

use App\Framework\Http\Status;
use App\Framework\Router\ActionResult;

final class WebSocketResult implements ActionResult
{
    /**
     * @var callable|null Handler für neue Verbindungen
     */
    private $onConnect = null;

    /**
     * @var callable|null Handler für eingehende Nachrichten
     */
    private $onMessage = null;

    /**
     * @var callable|null Handler für geschlossene Verbindungen
     */
    private $onClose = null;

    /**
     * @var callable|null Handler für Fehler
     */
    private $onError = null;

    /**
     * @var array Unterstützte Subprotokolle
     */
    private array $subprotocols = [];

    /**
     * @var int Maximale Nachrichtengröße in Bytes
     */
    private int $maxMessageSize = 1048576; // 1MB

    /**
     * @var int Ping-Intervall in Sekunden
     */
    private int $pingInterval = 30;

    public function __construct(
        public readonly Status $status = Status::SWITCHING_PROTOCOLS,
        public readonly array $headers = []
    ) {
    }

    /**
     * Setzt den Handler für neue Verbindungen
     *
     * @param callable $handler function(WebSocketConnection $connection): void
     */
    public function onConnect(callable $handler): self
    {
        $this->onConnect = $handler;

        return $this;
    }

    /**
     * Setzt den Handler für eingehende Nachrichten
     *
     * @param callable $handler function(WebSocketConnection $connection, string $message): void
     */
    public function onMessage(callable $handler): self
    {
        $this->onMessage = $handler;

        return $this;
    }

    /**
     * Setzt den Handler für geschlossene Verbindungen
     *
     * @param callable $handler function(WebSocketConnection $connection, int $code, string $reason): void
     */
    public function onClose(callable $handler): self
    {
        $this->onClose = $handler;

        return $this;
    }

    /**
     * Setzt den Handler für Fehler
     *
     * @param callable $handler function(WebSocketConnection $connection, \Throwable $error): void
     */
    public function onError(callable $handler): self
    {
        $this->onError = $handler;

        return $this;
    }

    /**
     * Setzt unterstützte Subprotokolle
     */
    public function withSubprotocols(array $subprotocols): self
    {
        $this->subprotocols = $subprotocols;

        return $this;
    }

    /**
     * Setzt die maximale Nachrichtengröße
     */
    public function withMaxMessageSize(int $bytes): self
    {
        $this->maxMessageSize = $bytes;

        return $this;
    }

    /**
     * Setzt das Ping-Intervall
     */
    public function withPingInterval(int $seconds): self
    {
        $this->pingInterval = $seconds;

        return $this;
    }

    // Getter
    public function getOnConnect(): ?callable
    {
        return $this->onConnect;
    }

    public function getOnMessage(): ?callable
    {
        return $this->onMessage;
    }

    public function getOnClose(): ?callable
    {
        return $this->onClose;
    }

    public function getOnError(): ?callable
    {
        return $this->onError;
    }

    public function getSubprotocols(): array
    {
        return $this->subprotocols;
    }

    public function getMaxMessageSize(): int
    {
        return $this->maxMessageSize;
    }

    public function getPingInterval(): int
    {
        return $this->pingInterval;
    }
}
