import dataiku
import os
import re
from datetime import datetime
import json

import dash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc

ANSWERS_FOLDER = dataiku.Folder("zhYE6r2h") # Folder to log answers and positive/negative reactions
LOG_ALL_ANSWERS = False # Whether all answers or only answers with user feedback should be logged
WEBAPP_NAME = "qanda_webapp" # Name of the app (logged when a conversation is flagged)
VERSION = "1.0" # Version of the app (logged when a conversation is flagged)

chain = dataiku.api_client().get_default_project().get_llm("agent:bR8FHxfn:v1")
llm_provided = len(dataiku.get_custom_variables()["LLM_id"]) > 0

ERROR_MESSAGE_MISSING_KEY = """
LLM connection missing. You need to add it as a user secret. Cf. this project's wiki

**Please note that the question answering web app is not live on Dataiku’s public project gallery but you can test it by downloading the project and providing an LLM connection**.

You can find examples of answers in the `answers` dataset.
"""          

def escape_markdown(text):
    return text.replace('\\`', '`').replace('\\_', '_')\
        .replace('\\~', '~').replace('\\>', '>')\
        .replace('\\[', '[').replace('\\]', ']')\
        .replace('\\(', '(').replace('\\)', ')')\
        .replace('`', '\\`').replace('_', '\\_')\
        .replace('~', '\\~').replace('>', '\\>').replace('[', '\\[')\
        .replace(']', '\\]').replace('(', '\\(').replace(')', '\\)')

def get_answer(query):
    """
    Provide the LLM with the query and chunks extracted from the source documents and get an answer.
    """
    completion = chain.new_completion()
    completion.with_message(query)
    response = json.loads(completion.execute().text)
    answer = f'{escape_markdown(response["result"])}'
    sources = "\n\n**Sources**:\n\n" + "\n\n".join([f"**{escape_markdown(x['unique_id'])}**: {escape_markdown(x['content'])}" for x in response["sources"]])
    return answer + sources

# Layout

STYLE_ANSWER = {
    "margin-top": "20px",
    "align-items": "flex-start",
    "display": "flex",
    "height": "auto"
}

STYLE_BUTTON = {
    "width": "20px",  
    "text-align": "center",
    "margin": "0px 5px",
}

STYLE_FEEDBACK = {
    "margin-left": "10px",
    "display": "none"
}

ok_icon_fill = html.Span(html.I(className="bi bi-emoji-smile-fill"))
nok_icon_fill = html.Span(html.I(className="bi bi-emoji-frown-fill"))
ok_icon = html.Span(html.I(className="bi bi-emoji-smile"))
nok_icon = html.Span(html.I(className="bi bi-emoji-frown"))

send_icon = html.Span(html.I(className="bi bi-send"))
question_bar = dbc.InputGroup(
    [
        dbc.Input(id='query', value='', type='text', minLength=0),
        dbc.Button(send_icon, id='send-btn', title='Get an answer')
    ],
    style = {"margin-top": "20px"}
)

app.title = "Question answering"
app.config.external_stylesheets = [
    dbc.themes.ZEPHYR,
    dbc.icons.BOOTSTRAP
]
app.layout = html.Div(
    [
        html.H4(
            "Question answering over documents",
            style={"margin-top": "20px", "text-align": "center"}
        ),
        dcc.Markdown("Get answers based on the [Global assessment report on biodiversity and ecosystem services (summary for policymakers)](https://www.ipbes.net/global-assessment) and the Wikipedia pages on [Biodiversity](https://en.wikipedia.org/wiki/Biodiversity), [CITES](https://en.wikipedia.org/wiki/CITES) and [IUCN](https://en.wikipedia.org/wiki/International_Union_for_Conservation_of_Nature) (retrieved May 2023)"),
        question_bar,   
        html.Div(
            [
                dbc.Spinner(
                    dcc.Markdown(
                        id='answer',
                        link_target="_blank",
                        style={"min-width": "100px"}
                    ),
                    color="primary"
                ),
                html.Div(
                    [
                        html.A(ok_icon, id="link_ok", href="#", style=STYLE_BUTTON),
                        html.A(nok_icon, id="link_nok", href="#", style=STYLE_BUTTON)   
                    ],
                    id="feedback_buttons",
                    style=STYLE_FEEDBACK)
            ],
            style=STYLE_ANSWER
        ),
        html.Div(id='debug'),
        dcc.Store(id='feedback', data=2, storage_type='memory'),
        dcc.Store(id='question', storage_type='memory'),
        dcc.Store(id='question_id', storage_type='memory'),
    ],
    style={
        "margin": "auto",
        "text-align": "left",
        "max-width": "800px"
    }
)

# Callbacks

@app.callback(
    Output('answer', 'children'),
    Output('feedback_buttons', 'style'),
    Output('question', 'data'),
    Output('question_id', 'data'),
    Input('send-btn', 'n_clicks'),
    Input('query', 'n_submit'),
    State('query', 'value'),
)
def answer_question(n_clicks, n_submit, query):
    """
    Display the answer
    """
    if len(query) == 0:
        return "", STYLE_FEEDBACK, query, 0
    start = time.time()
    if not llm_provided:
        return ERROR_MESSAGE_MISSING_KEY, STYLE_ANSWER, query, 0
    style = dict(STYLE_FEEDBACK)
    style["display"] = "flex"
    answer = f"{get_answer(query)}\n\n{(time.time()-start):.1f} seconds"
    return answer, style, query, hash(str(start) + query)

@app.callback(
    Output('debug', 'children'),
    Input('answer', 'children'),
    Input('link_ok', 'children'),
    Input('link_nok', 'children'),
    State('query', 'value'),
    State('question_id', 'data'),
    State('feedback', 'data'),
)
def log_answer(answer, ok, nok, query, question_id, feedback):
    """
    Log the question and the answer
    """
    if llm_provided and len(answer) > 0:
        path = f"/{str(question_id)}.json"
        if LOG_ALL_ANSWERS or feedback in [1, -1]:
            with ANSWERS_FOLDER.get_writer(path) as w:
                w.write(bytes(json.dumps({
                    "question": query,
                    "answer": answer,
                    "feedback": 0 if feedback == 2 else feedback,
                    "timestamp": str(datetime.now()),
                    "webapp": WEBAPP_NAME,
                    "version": VERSION
                }), "utf-8"))
        else:
            if path in ANSWERS_FOLDER.list_paths_in_partition():
                ANSWERS_FOLDER.delete_path(path)
    return ""

@app.callback(
    Output('link_ok', 'children'),
    Output('link_nok', 'children'),
    Input('feedback', 'data')
)
def update_icons(value):
    """
    Update the feedback icons when the user likes or dislikes an answer
    """
    ok = ok_icon_fill if value is not None and value == 1 else ok_icon
    nok = nok_icon_fill if value is not None and value == -1 else nok_icon
    return ok, nok

@app.callback(
    Output('feedback', 'data'),
    Input('link_ok', 'n_clicks'),
    Input('link_nok', 'n_clicks'),
    Input('send-btn', 'n_clicks'),
    Input('query', 'n_submit'),
    State('feedback', 'data'),
    State('query', 'value'),
    State('question', 'data'),
)
def provide_feedback(ts_ok, ts_nok, click, submit, value, question, previous_question):
    """
    Record the feedback of the user
    """
    triggered = dash.ctx.triggered_id
    if triggered == "link_ok":
        return 1 if value != 1 else 0
    elif triggered == "link_nok":
        return -1 if value != -1 else 0
    return value if question == previous_question else 2