# tools/knowledge_base.py
import requests
from tools.base import Tool

# ── Tool 1: Knowledge base search (RAG from Chapter 11) ──────────────
def _search_knowledge_base(query: str, max_results: int = 3) -> str:
    """
    Search the company knowledge base.
    In production, this calls your RAGSystem from Chapter 11.
    """
    # Replace with: rag.ask(query) from Chapter 11
    # For now, a stub that returns realistic-looking output
    return (
        f"Knowledge base results for '{query}':\n"
        f"1. [HR Policy v4.2, p.5] Annual leave: 25 days per year\n"
        f"2. [HR Policy v4.2, p.7] Remote working: up to 3 days/week"
    )

search_kb = Tool(
    name="search_knowledge_base",
    function=_search_knowledge_base,
    definition={
        "type": "function",
        "function": {
            "name": "search_knowledge_base",
            "description": "Search the company knowledge base for information
about HR policies, IT procedures, finance rules, and company guidelines.
Use this when a user asks a question that might be covered in internal
documentation. Query should be a natural language question or key phrase.",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {
                        "type": "string",
                        "description": "Natural language question or search phrase"
                    },
                    "max_results": {
                        "type": "integer",
                        "description": "Max results to return (1-10, default 3)",
                        "default": 3
                    }
                },
                "required": ["query"]
            }
        }
    }
)


# ── Tool 2: Get today’s date ─────────────────────
from datetime import date

def _get_current_date() -> str:
    return date.today().strftime("%A, %d %B %Y")

get_date = Tool(
    name="get_current_date",
    function=_get_current_date,
    definition={
        "type": "function",
        "function": {
            "name": "get_current_date",
            "description": "Get today's date. Use when the user asks what day or
date it is, or when calculating deadlines and time-sensitive information.",
            "parameters": {
                "type": "object",
                "properties": {},
                "required": []
            }
        }
    }
)


# ── Tool 3: Create a support ticket ─────────────────
def _create_support_ticket(summary: str, priority: str, category: str) -> str:
    """
    Create a support ticket in the ticketing system.
    In production, this calls your CRM or ticketing API.
    """
    ticket_id = f"TKT-{hash(summary) % 10000:04d}"
    return f"Ticket created: {ticket_id} | Priority: {priority} | Category: {category}"

create_ticket = Tool(
    name="create_support_ticket",
    function=_create_support_ticket,
    definition={
        "type": "function",
        "function": {
            "name": "create_support_ticket",
            "description": "Create a support ticket when a user has an issue that
cannot be resolved through the knowledge base. Use after checking the knowledge
base first. Only create a ticket if the user confirms they want one.",
            "parameters": {
                "type": "object",
                "properties": {
                    "summary": {
                        "type": "string",
                        "description": "One-sentence description of the issue"
                    },
                    "priority": {
                        "type": "string",
                        "enum": ["low", "medium", "high", "urgent"],
                        "description": "Issue priority based on business impact"
                    },
                    "category": {
                        "type": "string",
                        "enum": ["hr", "it", "finance", "facilities", "other"],
                        "description": "Issue category"
                    }
                },
                "required": ["summary", "priority", "category"]
            }
        }
    }
)
