from __future__ import annotations

import json
from http import HTTPStatus

from backend.features.mentor.agent import MentorAgentError


class MentorHttpMixin:
    def stream_mentor_chat(self) -> None:
        try:
            body = self.read_json_body()
            stream = self.application_service.mentor_stream(body)
        except (ValueError, json.JSONDecodeError) as exc:
            self.send_json({"error": str(exc)}, HTTPStatus.BAD_REQUEST)
            return
        self.send_response(HTTPStatus.OK)
        self.send_header("Content-Type", "application/x-ndjson; charset=utf-8")
        self.send_header("Cache-Control", "no-cache, no-transform")
        self.send_header("X-Accel-Buffering", "no")
        self.send_header("Connection", "close")
        self.end_headers()
        try:
            for event in stream:
                self._write_stream_event(event)
            self._write_stream_event({"type": "done"})
        except (ValueError, MentorAgentError) as exc:
            self._write_stream_event({"type": "error", "error": str(exc)})
        except (BrokenPipeError, ConnectionResetError):
            pass
        finally:
            self.close_connection = True
