#!/usr/bin/env python
"""
This module provides the InstructLabProvider class for enhancing text generation through an AI service. It utilizes the
requests library to send POST requests to an AI model server, with configurations fetched from environment variables.

The main functionality is encapsulated in the improve_text method, which takes a prompt and text, sends a request to
the AI service, and returns the improved text. An AiError exception is raised in case of request failures, detailing
the issue.

Classes:
- InstructLabProvider: A class for improving text using an AI model.

Attributes:
- url (str): The URL of the AI model server.
- model (str): The identifier of the AI model used for text generation.

Methods:
- __init__(self): Initializes the class with AI-related environment variables.
- improve_text(self, prompt: str, text: str) -> str: Improves the provided text using the AI model and returns the
enhanced text.

Exceptions:
- AiError: Raised when the POST request to the AI service fails, indicating a service issue.
"""

# pylint: disable=too-few-public-methods

import requests
from core.env_fetcher import EnvFetcher
from exceptions.exceptions import AiError
from providers.ai_provider import AIProvider


class InstructLabProvider(AIProvider):
    """
    This class provides a way to interact with an AI model hosted on a specified URL for improving text generation.

    Attributes:
    - url (str): The URL of the AI model server.
    - model (str): The name or identifier of the AI model used for text generation.

    Methods:
    - __init__(self): Initializes the class instance with AI-related environment variables.
    - improve_text(self, prompt: str, text: str) -> str: Sends a POST request to a specified URL with a given prompt
    and text to improve the text using a specific model. It then returns the improved text obtained from the response.
    Raises AiError if the POST request fails, providing details of the failure.
    """

    def __init__(self) -> None:
        """
        Initialize the class instance with AI-related environment variables.
        Arguments:
        - self: The class instance to be initialized.
        Return: None
        """
        self.url: str = EnvFetcher.get("AI_URL")
        self.model: str = EnvFetcher.get("AI_MODEL")

    def improve_text(self, prompt: str, text: str) -> str:
        """
        Sends a POST request to a specified URL with a given prompt and text to improve the text using a specific model.
        It then returns the improved text obtained from the response.

        Arguments:
        - prompt (str): The initial prompt to provide context for improving the text.
        - text (str): The text to be improved based on the provided prompt.

        Return:
        str: The improved text generated by the AI model in response to the provided prompt and text.

        Exceptions:
        - AiError: Raised when the POST request to the URL fails, indicating an issue with the AI service.
        """
        full_prompt: str = f"{prompt}\n\n{text}"
        response: requests.Response = requests.post(
            self.url,
            json={
                "model": self.model,
                "prompt": full_prompt,
                "stream": False,
                "options": {"temperature": 0.3},
            },
            timeout=60,
        )
        if response.status_code == 200:
            return response.json().get("response", "").strip()
        raise AiError(
            f"InstructLab request failed: {response.status_code} - {response.text}"
        )
