#
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 MindTenet LLC. All rights reserved.
# SPDX-License-Identifier: AGPL-3.0-or-later
#

"""Telnet-backed connector for the hosted server app."""

from __future__ import annotations

import hashlib
import time
from typing import Any

from undef.terminal.screen import decode_cp437
from undef.terminal.server.connectors.base import SessionConnector
from undef.terminal.transports.telnet import TelnetTransport

_COLS = 80
_ROWS = 25


class TelnetSessionConnector(SessionConnector):
    """Connect a hosted session to a remote telnet endpoint."""

    _VALID_CONFIG_KEYS: frozenset[str] = frozenset({"host", "port", "input_mode"})

    def __init__(self, session_id: str, display_name: str, config: dict[str, Any]) -> None:
        unknown = set(config) - self._VALID_CONFIG_KEYS
        if unknown:
            raise ValueError(f"unknown telnet connector_config keys: {sorted(unknown)}")
        self._session_id = session_id
        self._display_name = display_name
        self._host = str(config.get("host", "127.0.0.1"))
        self._port = int(config.get("port", 23))
        self._transport = TelnetTransport()
        self._connected = False
        self._input_mode = str(config.get("input_mode", "open"))
        self._paused = False
        self._received_bytes = 0
        self._screen_buffer = ""
        self._banner = f"Connected to telnet://{self._host}:{self._port}"

    def _screen(self) -> str:
        header = [
            f"\x1b[1;35m[{self._display_name} ({self._session_id})]\x1b[0m",
            "-" * 60,
            f"\x1b[32mUpstream:\x1b[0m telnet://{self._host}:{self._port}",
            f"\x1b[32mMode:\x1b[0m {'Shared input' if self._input_mode == 'open' else 'Exclusive hijack'}",
            f"\x1b[32mControl:\x1b[0m {'Paused for hijack' if self._paused else 'Live'}",
            f"\x1b[33m{self._banner}\x1b[0m",
            "",
        ]
        lines = (header + self._screen_buffer.splitlines())[-_ROWS:]
        return "\n".join(lines)

    def _snapshot(self) -> dict[str, Any]:
        screen = self._screen()
        lines = screen.splitlines() or [""]
        last = lines[-1]
        return {
            "type": "snapshot",
            "screen": screen,
            "cursor": {"x": min(len(last), _COLS - 1), "y": min(len(lines) - 1, _ROWS - 1)},
            "cols": _COLS,
            "rows": _ROWS,
            "screen_hash": hashlib.sha256(screen.encode("utf-8")).hexdigest()[:16],
            "cursor_at_end": True,
            "has_trailing_space": False,
            "prompt_detected": {"prompt_id": "telnet_stream"},
            "ts": time.time(),
        }

    def _hello(self) -> dict[str, Any]:
        return {"type": "worker_hello", "input_mode": self._input_mode, "ts": time.time()}

    async def start(self) -> None:
        await self._transport.connect(self._host, self._port)
        self._connected = True

    async def stop(self) -> None:
        await self._transport.disconnect()
        self._connected = False

    def is_connected(self) -> bool:
        return self._connected and self._transport.is_connected()

    async def poll_messages(self) -> list[dict[str, Any]]:
        if not self.is_connected():
            return []
        data = await self._transport.receive(4096, 100)
        if not data:
            return []
        self._received_bytes += len(data)
        text = decode_cp437(data)
        self._screen_buffer = (self._screen_buffer + text)[-32_000:]
        self._banner = f"Received {self._received_bytes} bytes from telnet upstream."
        return [{"type": "term", "data": text, "ts": time.time()}, self._snapshot()]

    async def handle_input(self, data: str) -> list[dict[str, Any]]:
        if self.is_connected():
            await self._transport.send(data.encode("cp437", errors="replace"))
            self._banner = f"Sent {len(data)} characters upstream."
        return [self._snapshot()]

    async def handle_control(self, action: str) -> list[dict[str, Any]]:
        if action == "pause":
            self._paused = True
            self._banner = "Exclusive control active."
        elif action == "resume":
            self._paused = False
            self._banner = "Exclusive control released."
        elif action == "step":
            self._banner = "Step requested. Awaiting upstream output."
        else:
            self._banner = f"Ignored control action: {action}"
        return [self._snapshot()]

    async def get_snapshot(self) -> dict[str, Any]:
        return self._snapshot()

    async def get_analysis(self) -> str:
        return "\n".join(
            [
                f"[telnet session analysis — worker: {self._session_id}]",
                f"host: {self._host}",
                f"port: {self._port}",
                f"input_mode: {self._input_mode}",
                f"paused: {self._paused}",
                f"bytes_received: {self._received_bytes}",
                f"connected: {self.is_connected()}",
            ]
        )

    async def clear(self) -> list[dict[str, Any]]:
        self._screen_buffer = ""
        self._banner = "Screen buffer cleared."
        return [self._snapshot()]

    async def set_mode(self, mode: str) -> list[dict[str, Any]]:
        if mode not in {"open", "hijack"}:
            raise ValueError(f"invalid mode: {mode}")
        self._input_mode = mode
        if mode == "open":
            self._paused = False
        self._banner = f"Input mode set to {'Shared input' if mode == 'open' else 'Exclusive hijack'}."
        return [self._hello(), self._snapshot()]
