from __future__ import annotations

import warnings
from functools import lru_cache
from importlib import resources
from typing import Any, Dict

from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_oci import ChatOCIGenAI
from langchain_core.runnables import RunnablePassthrough

warnings.filterwarnings("ignore")

from dotenv import load_dotenv
import os
load_dotenv()

class BaseClassifier:
    MODEL_NAME = "openai/gpt-oss-20b"
    API_KEY = "fake-key"
    BASE_URL = "http://10.153.34.154/gpt-oss-20b/v1"
    REASONING_EFFORT = "low"
    PROMPT_FILE: str = ""

    def __init__(self, *, temperature: float = 0.0) -> None:
        self.llm = ChatOCIGenAI(
            model_id=os.getenv("OCI_ENDPOINT_ID", ""),
            service_endpoint=os.getenv("OCI_ENDPOINT", ""),
            compartment_id=os.getenv("OCI_COMPARTMENT_ID", ""),
            auth_file_location=os.getenv("OCI_AUTH_FILE_LOCATION", "./config"),
            model_kwargs={"temperature": 0.0,
                        "top_p": 0.1,
                        "reasoning_effort":"MINIMAL"}
        )
        """self.llm = ChatOpenAI(
            model="openai/gpt-oss-20b",
            api_key="fake-key",
            base_url="http://10.153.34.154/gpt-oss-20b/v1",
            temperature=0.0,
            top_p=0.1,
            reasoning_effort="low"
        )"""
        
        self.prompt = PromptTemplate(
            template=self._load_prompt(),
            input_variables=["text"],
            partial_variables=self._partial_variables(),
        )

        self.chain = (
            {"text": RunnablePassthrough()}
            | self.prompt
            | self.llm
        )

    @classmethod
    @lru_cache
    def _load_prompt(cls) -> str:
        return resources.files("agent.prompts").joinpath(cls.PROMPT_FILE).read_text()

    def _partial_variables(self) -> Dict[str, Any]:
        return {}

    def run(self, text: str) -> Dict[str, Any]:
        raise NotImplementedError("Subclasses devem implementar o método run()")
