from typing import List
import requests
from sqlalchemy.orm import Session
from app.core.retrievers.base_retriever import Retriever

class RemoteRetriever(Retriever):
    """
    A retriever that fetches context from a single remote backend server.
    """
    def __init__(self, server_url: str):
        """
        Initializes the retriever with the remote server's URL.

        Args:
            server_url (str): The URL of the remote backend server.
        """
        self.server_url = server_url
        self.session = requests.Session()

    def retrieve_context(self, query: str, db: Session) -> List[str]:
        """
        Fetches context for a given query from the remote server.

        Args:
            query (str): The user's query string.
            db (Session): The database session (unused, but required by the
                          abstract base class).

        Returns:
            List[str]: A list of text strings representing the retrieved context.
        """
        try:
            # Send the query to the remote server
            response = self.session.post(self.server_url, json={'query': query})
            response.raise_for_status()  # Raise an exception for bad status codes

            # Assuming the server returns a JSON object with a 'context' key
            server_context = response.json().get('context', [])
            return server_context
        except requests.exceptions.RequestException as e:
            print(f"Error connecting to the server at {self.server_url}: {e}")
            return []