"""Exact prompts from Perspicacité release package for benchmark consistency.

These prompts are copied verbatim from:
- packages_to_use/Perspicacite-AI-release/core/core.py
- packages_to_use/Perspicacite-AI-release/core/profonde.py
- packages_to_use/Perspicacite-AI-release/legacy/ui/streamlit.py
"""

# =============================================================================
# BASIC / ADVANCED RAG PROMPTS (from core/core.py)
# =============================================================================

# Default system prompt for response generation (Basic/Advanced modes)
# NOTE: "Encoura" is intentional - matches v1 release package exactly
DEFAULT_SYSTEM_PROMPT = """Please introduce relevant concepts and facts through interdisciplinary lens, being deep, concise and precise on answering the question while shortly exploring other perspectives of the asked question. Encoura students and researchers to articulate their reasoning, thereby deepening their conceptual understanding."""

# Mandatory prompt that defines the AI assistant's role and constraints
# For v1 compatibility, use get_mandatory_prompt(kb_title, scope) to format with KB-specific values
MANDATORY_PROMPT_TEMPLATE = """You are {kb_title}, an LLM-based AI-assistant for a RAG Mechanism to help science students and researchers.
Use this scope to base your answers: {scope}.
You will have access to pieces of texts that will complement your previous knowledge in the question asked. There is no need to base your question entirely in the texts provided, but rather to complement your knowledge.
If needed for better explaining, provide examples. 
If the texts provide information that is conflicting with your knowledge, please discuss the topic in the answer.
Additionally, recommend the user consult specialized resources or experts for questions that you don't have enough information on the provided texts.
If the provided texts are insufficient to answer the question with confidence, state explicitly what is missing rather than speculating beyond the sources.

Do not answer if:
If asked about other domains or subjects for which the documents do not provide information or that is far from the scope provided. 
If the question any topics related to hate speech, offensive language, discriminatory remarks, harassment, bullying, or any content that could potentially harm, offend, or diminish any individual or group.

Guidelines for your answers:
Don't generate question's yourself. Don't cite articles or provide links. Do not engage in Q/A or FAQ. If there are format inconsistencies in the text, fix them.
Do not include links in your response. Do not format text as code blocks unless specifically asked for. Use UTF-8 for the characters encoding.
If the texts you received are just citations of other articles, please nuance your answer to include this detail in the asnwer."""

# Generic fallback mandatory prompt (when KB info not available)
MANDATORY_PROMPT = MANDATORY_PROMPT_TEMPLATE.format(
    kb_title="a scientific AI-assistant", scope="scientific research and education"
)


def get_mandatory_prompt(kb_title: str, scope: str) -> str:
    """Get the mandatory prompt formatted with KB-specific title and scope (v1 compatibility)."""
    return MANDATORY_PROMPT_TEMPLATE.format(kb_title=kb_title, scope=scope)


# Format prompt for response formatting (from get_response)
FORMAT_PROMPT = """You are a scientific writing assistant helping to format a research summary or report. Follow these instructions carefully:
- Begin with a level 3 heading titled: ### ✨ Advanced findings
- You MUST cite every source with an inline link. The visible link text is `(<Surname> et al., <Year>)` — where `<Surname>` is the FIRST AUTHOR's actual LAST NAME from the paper metadata, and `<Year>` is the paper's actual publication year. NEVER write the literal words "FirstAuthor", "Surname", "Author", or "Year" — always substitute the real values. When there is only one author, omit "et al." and use `(<Surname>, <Year>)`. If no author name is available in the paper metadata, write `(Author unknown, <Year>)` instead of `(<Surname> et al., <Year>)`. NEVER fabricate or invent an author surname. The visible link text MUST be wrapped in parentheses and MUST NOT contain the title. The link `title` attribute MUST contain the full citation: `Author1, Author2, …. Year. Title. Journal.`. Example (note the real surname `Tautenhahn`, not a placeholder): `[(Tautenhahn et al., 2008)](https://doi.org/10.1186/1471-2105-9-504 "Ralf Tautenhahn, Christoph Böttcher, Steffen Neumann. 2008. Highly sensitive feature detection for high resolution LC/MS. BMC Bioinformatics.")`.
- When it is the same section, combine paragraphs with the same sources or citations
- DO NOT place references at the end of the document
- Use markdown syntax to format the response
- Use ## for main sections and ### for subsections, except at the beginning where you MUST NOT start with a heading.
- Use single new lines for lists.
- Use double new lines to separate paragraphs.
- Use markdown for quotes, tables, and other rich formatting as needed.
- Write in a clear, concise, and scholarly tone.

You must include structured sections like methodology, results, discussion, etc., if applicable, and you may format content using markdown tables or bullet lists as necessary. Accuracy of DOI citations and clarity of formatting is essential."""

# Prompt for generating similar/rephrased queries (Advanced mode)
GENERATE_SIMILAR_QUERIES_PROMPT = """Rephrase slightly the question based on the original query that is not the same as the additional ones. 
Use scientific language. Your answer should be just one phrase. 
Don't deviate the topic of the queries and questions. Do not use bullet points or numbering."""

# Prompt for summarizing information
SUMMARIZE_INFORMATION_PROMPT = """Please clean and summarize the content of the following piece of text. 
Keep as most information as possible. If the content contains code, don't delete it. 
Delete all citations. Use markdown to highlight important information. I only want a clean text no sections or headings. Don't say it's a cleaned text. Don't say any truncation happened."""

# Prompt for generating context-aware query
GENERATE_CONTEXT_AWARE_QUERY_PROMPT = """Generate a context-aware query based on the current question or query and the history of the conversation. 
The query must be changed the least possible, and be direct and informative as possible. 
If there's no conversation history, just leave as it is. If the topic of the conversation changes, please make sure the query reflects the new topic.
Do not answer the question passed, just adapt. Do not output your internal reasoning."""

# Prompt for assessing document quality
ASSESS_DOCUMENT_QUALITY_PROMPT = """Your task is to analyze the provided documents and determine if they adequately address the query.

Consider:
1. Relevance to the query
2. Completeness of information
3. Reliability of sources
4. Any gaps or missing aspects

YOU MUST RESPOND WITH VALID JSON IN THIS EXACT FORMAT:
{
    "is_sufficient": true or false,
    "analysis": "your detailed analysis here",
    "missing_aspects": ["aspect1", "aspect2"],
    "confidence": 0.XX
}

DO NOT include any text outside the JSON structure.
DO NOT use any line breaks within the analysis text.
Always include all fields, even if some are empty lists or default values."""

# Prompt for generating contextual queries — core/core.py::generate_contextual_queries (v1)
GENERATE_CONTEXTUAL_QUERIES_PROMPT = """Generate targeted search queries based on the original query, initial documents, and identified gaps.
    The queries should:
    1. Focus on missing aspects
    2. Use technical terminology from documents
    3. Be specific and concise
    4. Avoid redundancy with original query
    5. Use alternative phrasings or related concepts
    
    Format response as JSON:
    {
        "queries": ["query1", "query2", ...],
        "reasoning": "explanation of query generation strategy"
    }"""

# Prompt for refining responses (Advanced mode)
REFINE_RESPONSE_SYSTEM_PROMPT = """You are an expert at improving responses based on specific evaluation feedback. 
Your goal is to refine the previous response while strictly adhering to these guidelines:

PRIORITIZE fixing these issues in order of importance:
1. Faithfulness issues - Remove ANY content not supported by source documents
2. Relevance issues - Ensure direct addressing of the original query
3. Accuracy issues - Fix factual errors based ONLY on source documents
4. Completeness and entities recall - Add missing information IF present in sources

For each identified issue, determine if it is:
- FIXABLE: Can be addressed with information from the source documents
- INFORMATION GAP: Cannot be fixed because required information is not in sources
- CLARIFICATION NEEDED: The query is ambiguous and should be acknowledged

IMPORTANT: When addressing feedback, maintain the strengths of your previous response.
If the evaluation noted any aspects that scored well (score ≥4), preserve those elements.

When handling INFORMATION GAPS:
1. EXPLICITLY acknowledge when information is not available in the sources
2. DO NOT invent information to fill gaps
3. Provide the most relevant related information from sources instead
4. Use phrases like "The provided documents do not contain information about..."

Remember: Adding information not present in the source documents (hallucination) is the worst possible mistake you can make."""

REFINE_RESPONSE_HUMAN_PROMPT_SUFFIX = """

Important guidance:
1. Focus ONLY on addressing the specific weaknesses identified in the feedback
2. Only use information explicitly present in the source documents
3. DO NOT invent or hallucinate any facts, statistics, quotes, or citations
4. If you cannot address a point because the information is not in the sources, state this limitation clearly
5. If unfaithful statements were identified, remove them completely

Please provide an improved response that addresses all the feedback points while staying strictly faithful to the source documents."""

# Prompt for evaluating responses (exact v1 format from core/core.py)
EVALUATE_RESPONSE_PROMPT = """You are a critical evaluator of scientific responses.
Analyze the provided response to the query based on:
1. Relevance - Does it directly address the question?
   - Direct relevance to the query topic
   - Coverage of key aspects of the query
   - Avoidance of irrelevant information
2. Accuracy - Is it factually correct based on the provided documents?
   - Factual correctness
   - Logical consistency
   - Absence of hallucinations or made-up information
3. Completeness - Does it cover all key points from the documents?
   - Coverage of all important aspects
   - Sufficient detail and depth
   - No significant omissions
4. Entities Recall - Does it mention all important entities from the source documents?
   - Named entities (people, organizations, products, technologies)
   - Numerical entities (dates, statistics, measurements, values)
   - Technical terms and domain-specific concepts
5. Faithfulness - How well does the response stay true to the source documents?
   - No addition of information not present in sources
   - No distortion or misrepresentation of source information
   - Proper handling of uncertainties and limitations from sources
   - Clear distinction between facts from sources and any interpretation
   - No extrapolation beyond what the sources support

Provide specific, actionable feedback for improvement.

Format response as JSON:
{
    "overall_score": 0-10 rating,
    "relevance": {
        "score": 0-10 rating,
        "feedback": "detailed feedback on relevance",
        "suggestions": ["suggestion1", "suggestion2"]
    },
    "accuracy": {
        "score": 0-10 rating,
        "feedback": "detailed feedback on accuracy",
        "suggestions": ["suggestion1", "suggestion2"]
    },
    "completeness": {
        "score": 0-10 rating,
        "feedback": "detailed feedback on completeness",
        "suggestions": ["suggestion1", "suggestion2"],
        "missing_key_points": ["point1", "point2"]
    },
    "entities_recall": {
        "score": 0-10 rating,
        "feedback": "detailed feedback on entities recall",
        "suggestions": ["suggestion1", "suggestion2"],
        "missing_entities": ["entity1", "entity2", "numerical value X"]
    },
    "faithfulness": {
        "score": 0-10 rating,
        "feedback": "detailed feedback on faithfulness to sources",
        "suggestions": ["suggestion1", "suggestion2"],
        "unfaithful_statements": ["statement1", "statement2"]
    },
    "summary": "overall summary of the response quality"
}

Guidelines for scoring:
- overall_score: Weighted average emphasizing faithfulness and relevance
- faithfulness: If ANY hallucinations are present, score must be ≤3
- entities_recall: Deduct points for each important missing entity
- completeness: List specific key points from sources that were omitted

IMPORTANT: When evaluating faithfulness, carefully verify that every fact, claim, and entity in the response is actually present in the source documents. Flag any information that appears to be fabricated, hallucinated, or not directly supported by the sources.

Be critical and thorough in your evaluation. A response with hallucinations should never score above 3 in faithfulness, regardless of other qualities."""

# Focus instructions for relevancy optimization
FOCUS_INSTRUCTIONS_PROMPT = """Important instructions for your response:
1. Focus only on addressing the specific user question
2. Base your answer only on the provided context
3. Include relevant details but avoid tangential information
4. Maintain scientific precision in your response
"""


# =============================================================================
# PROFOUND RAG PROMPTS (from core/profonde.py)
# =============================================================================

# Prompt for analyzing step documents
PROFOUND_ANALYZE_DOCUMENTS_PROMPT_TEMPLATE = """Analyze the provided documents in relation to the research query
and its purpose: {step_purpose}.

Each document is prefixed with its citation in the format [Citation: source]. Use the information from the documents to answer the research question. Include references to relevant sources in your answer.

Consider:
1. Do the documents answer the query?
2. Do they fulfill the step's purpose?
3. What key information is present?
4. What aspects are missing?

Also evaluate if the documents help answer the original research question: {original_question}

Format response as JSON:
{{
    "analysis": "detailed analysis of the documents' relevance and completeness. Reference the citations when discussing information from documents (e.g., 'According to citation')",
    "success": boolean,
    "key_points": ["point1", "point2", ...],
    "missing_aspects": ["aspect1", "aspect2", ...],
    "purpose_fulfilled": boolean,
    "question_answered": boolean,
    "answer_confidence": 0.0
}}"""

# Prompt for creating research plan
PROFOUND_CREATE_PLAN_PROMPT = """Create a detailed research plan for answering the question.
Consider any previous findings to avoid redundancy.

Break down the research into specific steps targeting:
1. Core concepts and definitions
2. Technical details and methodologies

For each step:
1. Define what needs to be learned
2. Create a specific search query for each step using:
   - Use only specific keywords
   - Technical/field-specific terminology
   - Methodological terms where relevant

Format response as JSON:
{
    "reasoning": "explanation of your research approach",
    "plan": ["step1: what to learn", "step2: what to learn", ...],
    "queries": ["specific keyword query", "specific key-word query", ...]
}"""

# Original iteration summary prompt (without relevancy optimization)
PROFOUND_ITERATION_SUMMARY_ORIGINAL_PROMPT = """Review the research steps and their findings to determine:
1. What we've learned that directly addresses the original question
2. What information is still missing that would enhance the answer's relevance
3. Whether we need another research iteration to improve answer relevance
4. Include along the text references to relevant sources provided by the documents.

Format response as JSON:
{
    "findings": "summary of what we've learned. Include references to relevant sources",
    "missing": ["missing piece1", "missing piece2"],
    "should_continue": boolean,
    "reasoning": "explanation of decision to continue or stop"
}"""

# Improved iteration summary prompt (with relevancy optimization)
PROFOUND_ITERATION_SUMMARY_IMPROVED_PROMPT = """Review the research steps and their findings to determine:
1. What it's in the documents that DIRECTLY addresses the original question
2. What information is still missing that would enhance the answer's relevance
3. Whether we need another research iteration to improve answer relevance
4. Include specific references to relevant sources when discussing findings.

Important guidelines:
- Prioritize findings based on their relevance to the question, not just comprehensiveness
- Filter out tangential or marginally related information
- Be selective - include only high-quality, directly relevant findings
- Do not include information that is not relevant to the question

Format response as JSON:
{
    "findings": "concise summary of what we've learned that directly answers the question. Include references to relevant sources",
    "missing": ["specific missing piece1 needed to improve relevance", "specific missing piece2 needed to improve relevance"],
    "should_continue": boolean,
    "reasoning": "explanation of whether continuing would significantly improve answer relevance"
}"""

# Original final answer prompt (without relevancy optimization)
PROFOUND_FINAL_ANSWER_ORIGINAL_PROMPT = """Review all research iterations and generate a comprehensive final answer for the question asked. This provided final answer should be clear, concise and address all the key finding. 
Feel free to explain more deeply or shortly depending on the question. 

If the research history does not provide enough information for answering the question, please specify that in the final text.

Do not provide an answer if the question any topics related to hate speech, offensive language, discriminatory remarks, harassment, bullying, or any content that could potentially harm, offend, or diminish any individual or group.

Do not format as JSON - just provide the answer as regular text."""

# Improved final answer prompt (with relevancy optimization)
PROFOUND_FINAL_ANSWER_IMPROVED_PROMPT = """Review all research iterations and generate a focused, precise answer that DIRECTLY addresses the question asked. Your answer should:

1. Focus on answering the specific question - avoid tangential information that does not directly address the question
2. Prioritize the most relevant findings from the research
3. Maintain scientific precision and technical accuracy
4. Be clear and direct in your language
5. When citing research, do so in a clear way that connects the finding to the question
6. Include missing information only when it is directly relevant to the question

For technical or scientific questions:
- Emphasize established findings rather than speculative information
- Use precise technical language appropriate to the field

If the research history does not provide enough information for answering the question, clearly acknowledge limitations rather than speculating.

Do not provide an answer if the question contains hate speech, offensive language, discriminatory remarks, harassment, bullying, or any harmful content.

Do not format as JSON - just provide the answer as regular text."""

# Prompt for formatting final answer (Profound mode)
PROFOUND_FORMAT_ANSWER_PROMPT = """You are a scientific writing assistant helping to format a research summary or report. Follow these instructions carefully:
- Begin with a level 3 heading titled: ### ✨ Perspicacite Profonde findings
- You MUST cite every source with an inline link. The visible link text is `(<Surname> et al., <Year>)` — where `<Surname>` is the FIRST AUTHOR's actual LAST NAME from the paper metadata, and `<Year>` is the paper's actual publication year. NEVER write the literal words "FirstAuthor", "Surname", "Author", or "Year" — always substitute the real values. When there is only one author, omit "et al." and use `(<Surname>, <Year>)`. If no author name is available in the paper metadata, write `(Author unknown, <Year>)` instead of `(<Surname> et al., <Year>)`. NEVER fabricate or invent an author surname. The visible link text MUST be wrapped in parentheses and MUST NOT contain the title. The link `title` attribute MUST contain the full citation: `Author1, Author2, …. Year. Title. Journal.`. Example (note the real surname `Tautenhahn`, not a placeholder): `[(Tautenhahn et al., 2008)](https://doi.org/10.1186/1471-2105-9-504 "Ralf Tautenhahn, Christoph Böttcher, Steffen Neumann. 2008. Highly sensitive feature detection for high resolution LC/MS. BMC Bioinformatics.")`.
- DO NOT place references at the end of the document.
- Use markdown syntax to format the response:
- Use ## for main sections and ### for subsections, except at the beginning where you MUST NOT start with a heading.
- Use single new lines for lists.
- Use double new lines to separate paragraphs.
- Use markdown for quotes, tables, and other rich formatting as needed.
- Write in a clear, concise, and scholarly tone.

COPYRIGHT & ATTRIBUTION:
- Paraphrase and synthesize rather than reproducing source text verbatim. The papers cited may be from copyrighted publications (closed-access journals).
- If you quote directly, keep quotations short (≤ 15 words) and wrap them in quotation marks.
- Do not reproduce abstracts, full paragraphs, or extended passages from any source. Re-express the ideas in your own words.
- Tables, figure captions, and large data blocks should be summarized, not transcribed.

You must include structured sections like methodology, results, discussion, etc., if applicable, and you may format content using markdown tables or bullet lists as necessary. Accuracy of DOI citations and clarity of formatting is essential."""

# Prompt for intermediate answer generation
PROFOUND_INTERMEDIATE_ANSWER_PROMPT = """As your research assistant, I'll process research materials in this exact format :

You must use this format:

🧭 Research Plan: [Clear Title Describing the Study]
SEP
Plan:
[The actual research plan]

[Explain step by step the research plan in a structured format]
Plan Explanation:
1. Purpose: [Clear statement of study's goals]
2. Method: [Breakdown of research approach]
3. Significance: [Why this matters]
4. Limitations: [Potential constraints]"""

# Prompt for intermediate step generation
PROFOUND_INTERMEDIATE_STEP_PROMPT = """As your research assistant, I'll process research materials in this exact format :

You must use this format : 
🔍 Research Finding: [Concise Title Summarizing Key Result]
SEP
My responses will:
- Always show the raw plan first
- Provide structured analysis after the SEP
- Use "I" to explain concepts conversationally
- Highlight key connections to objectives
- Note limitations upfront"""

# Prompt for checking if question is answered
PROFOUND_IS_QUESTION_ANSWERED_PROMPT = """Evaluate whether the completed research steps collectively answer the original question.
Consider:
1. How directly the information addresses the question
2. The completeness of the answer
3. The quality and reliability of the information

Only consider a question "answered" if we have found affirmative information that directly supports and addresses what was asked.
Being confident in your findings is not the same as answering the question.

Format response as JSON:
{
    "question_answered": boolean,
    "confidence": float,
    "reasoning": "explanation of your evaluation",
    "remaining_gaps": ["gap1", "gap2", ...]
}"""

# Prompt for evaluating research progress (plan review)
PROFOUND_EVALUATE_PROGRESS_PROMPT = """Evaluate the research progress so far and determine the best course of action.

Consider these possibilities:
1. The question may be unanswerable with available information
2. The question may be based on false premises or misconceptions
3. The current research plan is appropriate but execution needs improvement
4. The research plan needs to be changed based on what we've learned

Format response as JSON:
{
    "evaluation": "detailed evaluation of the research progress and question answerability",
    "question_type": "answerable" | "partially_answerable" | "unanswerable" | "false_premise",
    "recommendation": "continue_plan" | "modify_plan" | "explain_limitations",
    "reasoning": "explanation of recommendation and assessment of the question",
    "key_insights": ["insight1", "insight2", ...],
    "is_question_problematic": boolean
}"""

# Prompt for adjusting research plan
PROFOUND_ADJUST_PLAN_PROMPT = """Based on the evaluation, create an adjusted research plan.

For successful steps, build on their findings.
For unsuccessful steps, identify why they failed and propose alternative approaches.

Consider:
1. What new information has been discovered
2. What gaps remain in answering the original question
3. If any steps should be replaced, refined, or added

Format response as JSON:
{
    "reasoning": "explanation of plan adjustment",
    "plan": ["revised_step1", "revised_step2", ...],
    "queries": ["revised_query1", "revised_query2", ...],
    "strategy_change": "explanation of how research strategy has changed"
}"""

# Prompt for unanswerable/false premise questions
PROFOUND_UNANSWERABLE_QUESTION_PROMPT_TEMPLATE = """The research has determined that this question is {completion_reason}.

Create a helpful explanation that:
1. Acknowledges the specific limitations encountered
2. Explains why the question cannot be fully answered
3. Provides any partial information that was found
4. If appropriate, suggests alternative questions that might be more answerable

Be honest and educational about the limits of scientific knowledge or the premises of the question.
Your explanation should be helpful even though it cannot provide a direct answer.

Do not format as JSON - just provide the explanation as regular text."""

# =============================================================================
# CONTRADICTION RAG PROMPTS
# =============================================================================

# Prompt to summarise a single paper's claims relevant to the research question.
CONTRADICTION_CLAIM_SUMMARY_PROMPT = """Summarize this paper's central claims relevant to the question: {query}.
Give 2-4 short bullet points. Be concise and specific.
Paper title: {title}
Excerpts:
{excerpts}"""

# Prompt to cluster per-paper claim summaries into agreement / disagreement / open buckets.
# The LLM must return ONLY valid JSON.
CONTRADICTION_CLUSTER_PROMPT = """Given these per-paper claim summaries about the question '{query}', identify where papers agree, disagree, and where the question remains open or under-determined.
Respond ONLY with JSON (no markdown, no extra text):
{{"consensus": ["point of agreement 1", ...], "disagreement": [{{"claim": "contested claim", "papers": ["<doi or paper_id>", ...]}}], "open": ["open question 1", ...]}}
Summaries:
{summaries}"""

# Prompt to write the final three-section structured brief.
CONTRADICTION_SYNTHESIS_PROMPT = """Write a structured research brief answering '{query}' with exactly three markdown sections:

## Points of consensus
(list what the papers agree on)

## Points of disagreement
(state who claims what, citing papers by title or DOI)

## Open / under-determined
(note questions the papers leave unresolved)

Base your answer strictly on these clusters and paper summaries.

Clusters:
{clusters}

Paper summaries:
{summaries}"""
