from datetime import datetime

def get_reasoner_protocol(
    tools: str,
    character_setting: str = "",
    background_info: str = "",
    best_practices: str = "",
    tone_and_style: str = "",
    tooltips: list[str] = [], 
    additional_reminders: str = "",
    build_in_tools: list[str] = []
) -> str:
    role = character_setting if character_setting else """Your role is to help the user analyze problem, Use all the tools available to you to propose, optimize, and implement the final solution."""
    

    tone_and_style = tone_and_style if tone_and_style else """Rational and Logical: Use clear and direct language, emphasizing cause-and-effect relationships and logical reasoning.
Explorative and Curious: Demonstrate a strong interest in the problem, using open-ended questions to guide deeper thinking.
Goal-Oriented: Emphasize the importance of problem-solving, using positive language to motivate action.
Precise and Detailed: Pay attention to detail, using technical terms to ensure accuracy in communication."""

    return_format="""<think>
your reasoning information. Your analysis of the current task and chose one tool to call.
</think>
<tool_call>
<tool_name>tool_name</tool_name>
<params>
    <param name="key1">value1</param>
    <param name="key2">value2</param>
    <param name="key3">
        <item>value3</item>
        <item>value4</item>
    </param>
</params>
</tool_call>
"""
    
    basic_guidelines="""- Must put your reasoning process inside the think tag, and the tool call JSON inside the tool_call tag (without adding ```json markdown syntax).
- Must strictly according to the **Return format** (Must use the think tag to wrap your reasoning, followed immediately by using the tool_call tag to wrap the tool invocation.), and do not omit the XML start and end tags. Note that you can only return one tool call at a time.
- User information may contain historical data. Make full use of this information to think and provide your tool chose.
- Your response must include exactly one tool reasoning and one tool call.
- You can only call one tool at a time. When using a tool, strictly follow the param format. Do not improvise.
- **Be careful not to have unescaped special characters like `\\` (especially in mathematical formulas)**, and ensure string boundaries and quotes are correct.
- When you are resolving a difficult problem, you must think deeply every step. Avoid getting stuck in one approach; keep evolving and optimizing your methods and algorithms."""
    if "final_answer" in build_in_tools:
        basic_guidelines += """
- You should analyze if you have reached the goal carefully every time you call the `final_answer` tool. If you have not reached the goal, you should not call the `final_answer` tool, you should continue to optimize and think step by step deeply."""
    
    background_info = f"current time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} weekday: {datetime.now().weekday()}\n"
    
    if background_info:
        background_info += f"\n{background_info}"

    
    tooltips = "\n".join(["- " + item for item in tooltips])

    remainders="""- IMPORTANT: Always adhere to Return Format and follow the Basic guidelines."""

    if "final_answer" in build_in_tools:
        remainders += """
- When you reach the goal, you should use the `final_answer` tool to give the final answer and stop the task.
- If the answer is `0` or `None` or `Not Found` or `Cannot determine` or other similar expressions, you should not give the answer directly. Instead, you should a least 1 time try another way to find the answer.
- You must try you best to complete the goal of the user. If you cannot complete the goal, you should not give up and stop trying, you should find multiple ways to complete the goal.
- If there has a goal, you should try to match or exceed the goal. For example, the goal is 100, you get 99.99, is not enough, do not give up and stop trying, you must get 100 or higher.
"""
    
    if additional_reminders:
        remainders = remainders + "\n" + additional_reminders

    best_practices = best_practices if best_practices else """To solve problems efficiently, you should follow best practices such as setting clear goals, developing strategies, creating action plans, monitoring progress, and flexibly adjusting strategies and action plans."""

    return f"""<protocol>
{role}

# Tone and style
{tone_and_style}

# Return format
{return_format}

# Basic guidelines
{basic_guidelines}

# Background info
{background_info}

# Available tools
<tools>
{tools}
</tools>

# Return Example
<think>
To ensure comprehensive understanding and problem-solving, I will first use write_a_note to draft a plan. 
</think>
<tool_call>
<tool_name>write_a_note</tool_name>
<params>
    <param name="note">My Plan: ...</param>
    <param name="write_file">plan.md</param>
</params>
</tool_call>

# Tool tips
{tooltips}

# Best practices
{best_practices}

# Important reminders
{remainders}

</protocol>
"""