from typing import Type, List

from pydantic import BaseModel, Field
from sqlalchemy.orm import Session

from app.core.tools.base_tool import BaseTool
from app.models.run import Run
from app.services.file.file import FileService
from app.services.assistant.assistant import AssistantService

class FileSearchToolInput(BaseModel):
    query: str = Field(
        ...,
        description="query to look up in retrieval",
    )
    full_text_search: bool = Field(
        default=False,
        description="If True, performs full text search instead of snippet matching. "
                   "Use when user requests complete content analysis rather than just keyword matching"
    )

class FileSearchTool(BaseTool):
    name: str = "file_content_processor"
    description: str = (
        "Triggers when user requests summary/overview of uploaded files OR references specific file content. "
        "Explicit trigger phrases include: '帮我总结', '总结一下', '文件的内容有什么', '文件说了什么', 'summarize', 'overview', 'what's in the files'. "
        "Behavior rules: "
        "1. For ambiguous requests (e.g. '总结文档'), process only the most recent file batch by default "
        "2. Requires explicit user instruction (e.g. '包括所有批次') to process older batches "
        "3. Strict singleton operation: 1 invocation maximum per API call "
        "4. Always verify file availability before triggering"
        #    "Can be used to search through content of files uploaded by the user."
        #    + "If the user references specific file content (e.g., 'in my uploaded document...'), this function should be triggered."
        #    + "If the user has uploaded files in multiple batches, and their request is ambiguous (e.g. 'summarize the documents'), default to summarizing only the most recent batch of uploaded files. If the user's intent is to include older files, they should specify this explicitly."
        #   + "Singleton operation: Strictly 1 invocation per API call"
    )
    args_schema: Type[BaseModel] = FileSearchToolInput

    def __init__(self) -> None:
        super().__init__()
        self.__filenames = []
        self.__keys = []
        self.__dirkeys = []
        self.loop = None
        self.index = 0

    def configure(self, session: Session, run: Run, **kwargs):
        # 获取当前事件循环
        # document_id = []
        file_key = []
        # filesinfo = []
        # 后语要从知识库里选择文件，所以在openassistant的数据库里可能不存在
        for key in run.file_ids:
            if len(key) == 36:
                self.__keys.append(key)  # 添加文件id 作为检索
            else:
                file_key.append(
                    key
                )  ## assiatant的id数据，在r2r里没办法检索需要提取filekey字段

        print(
            "document_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_iddocument_id"
        )
        # print(document_id)
        print(file_key)
        files = []
        # 这种情况是uuid.ex 这种格式的在最早的时候存在的，后续要去掉
        if len(file_key) > 0:
            ## 获取文件信息
            files = FileService.get_file_list_by_ids(session=session, file_ids=file_key)
            for file in files:
                self.__keys.append(file.key)
            print(files)
        """
        # 读取assistant的数据，获取文件夹的id
        db_asst = AssistantService.get_assistant_sync(
            session=session, assistant_id=run.assistant_id
        )

        if db_asst.tool_resources and "file_search" in db_asst.tool_resources:
            ##{"file_search": {"vector_store_ids": [{"file_ids": []}]}}
            asst_folder_ids = (
                db_asst.tool_resources.get("file_search")
                .get("vector_stores")[0]
                .get("folder_ids")
            )
            print(asst_folder_ids)
            # folder_fileinfo = []
            if asst_folder_ids:
                self.__dirkeys = asst_folder_ids
        """
        # pre-cache data to prevent thread conflicts that may occur later on.
        print(
            "---------ssssssssssss-----------------sssssssssssss---------------ssssssssssssss-------------sssssssssssss-------------ss-------"
        )
        print(self.__dirkeys)
        print(self.__keys)

    # indexes: List[int],
    def run(self, query: str,full_text_search:bool=False) -> dict:
        print(
            "file_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keysfile_keys"
        )

        print(self.__keys)
        print(self.__dirkeys)
        files = []
        try:
            if full_text_search:
                files = FileService.list_chunks(ids=self.__keys)
            elif self.index == 0 and query:
                ## 必须有总结的内容query和才能触发
                files = FileService.search_in_files(
                    query=query, file_keys=self.__keys, folder_keys=self.__dirkeys
                )
                self.index = 1
        except Exception as e:
            print(e)
        # print(files)
        return files

    def instruction_supplement(self) -> str:
        """
        为 Retrieval 提供文件选择信息，用于 llm 调用抉择
        """
        if (self.__keys and len(self.__keys) > 0) or (
            self.__dirkeys and len(self.__dirkeys) > 0
        ):
            return ""
        else:
            return "如果您不确定用户发的文件内容或者代码库结构，请使用file_search工具读取内容并收集相关信息，不要瞎猜或者编造答案。"
