"""Licium tools for LangChain, CrewAI, AutoGen integration."""

from __future__ import annotations

from typing import Any, Optional

from .client import Licium


class LiciumTool:
    """Licium as a tool for AI agent frameworks.

    Works with LangChain, CrewAI, AutoGen, or any framework that
    accepts a tool with name/description/run.

    Usage with LangChain:
        from licium import LiciumTool
        from langchain.agents import create_react_agent

        agent = create_react_agent(llm, tools=[LiciumTool()])
        agent.invoke("Tesla SEC 10-K financial data")

    Usage with CrewAI:
        from licium import LiciumTool
        from crewai import Agent

        agent = Agent(role="Analyst", tools=[LiciumTool()])
    """

    name: str = "licium"
    description: str = (
        "Execute any task through Licium's specialist agent network. "
        "Gets real-time data: stock prices, crypto, FDA drugs, SEC filings, "
        "court records, clinical trials, PubMed, and any public API. "
        "Input: a task description string."
    )

    def __init__(
        self,
        base_url: str = "https://www.licium.ai",
        api_key: Optional[str] = None,
        max_steps: int = 7,
        cost_preference: str = "balanced",
    ):
        self.client = Licium(base_url=base_url, api_key=api_key)
        self.max_steps = max_steps
        self.cost_preference = cost_preference

    def run(self, task: str) -> str:
        """Execute task and return result text."""
        return self.client.delegate_result(
            task,
            max_steps=self.max_steps,
            cost_preference=self.cost_preference,
        )

    # LangChain compatibility
    def _run(self, task: str) -> str:
        return self.run(task)

    async def _arun(self, task: str) -> str:
        # sync fallback — async는 나중에
        return self.run(task)

    # CrewAI compatibility
    def __call__(self, task: str) -> str:
        return self.run(task)
