import gradio as gr
import pandas as pd
import gspread
from google.auth import default
import requests
import time
import json
import re

# Authenticate and authorize with Google Sheets
# creds, _ = default()
# gc = gspread.authorize(creds)
gc = gspread.service_account(filename="botresponse-6f1a8c749aa0.json")

# Specify your Google Sheets credentials, sheet_id, and worksheet_name
sheet_id = "18hnoTsEaGMWMael42MXubb-FzAe5jJB5RpaSolIXyb0"
worksheet_name = "Sheet1"


# Function to get the initial response
def get_initial_response(input_message):
    url = "https://itell-api.learlab.vanderbilt.edu/chat"

    payload = {"textbook_name": "think-python-2e", "message": input_message}
    headers = {"Content-Type": "application/json"}

    # Measure the start time
    start_time = time.time()

    response = requests.post(url, json=payload, headers=headers)
    data = json.loads(response.text)
    message = data["message"]
    message = message.encode("utf-8").decode("unicode_escape")
    message = re.sub(r"^[\s'\"]*(bot|cite sources)?[\s'\"]*", "", message)
    message = message.strip(" ':\"")

    # Calculate the elapsed time
    elapsed_time = time.time() - start_time
    elapsed_time = round(elapsed_time, 2)

    return {
        bot_resp: message,
        response_time: elapsed_time,
    }


# Function to collect feedback and update the spreadsheet
def feedback_response(
    input_message,
    bot_message,
    response_time,
    feedback_primary,
    feedback_secondary,
    comments,
):
    feedback = []
    feedback.append(feedback_primary)
    feedback.extend(feedback_secondary)

    # Update Google Sheets
    sh = gc.open_by_key(sheet_id)
    worksheet = sh.worksheet(worksheet_name)

    # Create a DataFrame from the response and additional comments
    df = pd.DataFrame(
        {
            "Input Message": [input_message],
            "Output": [bot_message],
            "Time took in Seconds": [response_time],
            "Feedback": [str(feedback)],
            "Additional Comments": [comments],
        }
    )

    # Append the data to the worksheet
    worksheet.append_rows(df.values.tolist())
    gr.Info("Feedback Submitted")
    return {
        primary_feedback: None,
        secondary_feedback: None,
        additional_comments: None,
    }


with gr.Blocks(title="iTELL Chat Feedback") as feedback_interface:
    title = "Itell Guide Response Bot"
    gr.components.Markdown(
        f"<h1 style='text-align: center; margin-bottom: 1rem'>{title}</h1>"
    )
    gr.Markdown(
        """
    # Introduction
    This is an interface to test iTELL's guide on the side. Please be aware that responses can take up to 20 seconds.
    # Step by Step Introduction
    1. Place a question in the input message textbox.
    2. Wait 10 ~ 20 seconds for the response to appear on the right.
    3. After looking at the results, provide primary feedback on the response.
    4. If desired, add secondary feedback selections: Informative, Inaccurate, Nonsense.
    4. Write down additional comments for more feedback.
    5. Press "Submit Feedback".
    """
    )
    elapsed_time = gr.State(0)
    with gr.Row():
        with gr.Column():
            usr_msg = gr.Textbox(interactive=True, label="Input Message", lines=7)
            response_time = gr.Number(label="Response Time", step=0.01)
            bot_resp = gr.Textbox(label="Output", type="text", lines=10)
            with gr.Row():
                clear_btn = gr.ClearButton(usr_msg, value="Clear")
                submit_btn = gr.Button("Submit")
                # submit_btn.click(get_initial_response, inputs=usr_msg, outputs=bot_resp)
        with gr.Column():
            primary_feedback = gr.Radio(
                ["👍 good", "👎 bad", "❌ inappropriate"], label="Primary Feedback"
            )
            secondary_feedback = gr.CheckboxGroup(
                [
                    "🧠 Informative",
                    "🙅 Inaccurate",
                    "❓ Nonsense",
                    "🩶 Character Encoding Error",
                ],
                label="Secondary Feedback",
            )
            additional_comments = gr.Textbox(
                label="Additional Comments", interactive=True, lines=7
            )
            feedback_btn = gr.Button("Submit Feedback")
            feedback_btn.click(
                feedback_response,
                inputs=[
                    usr_msg,
                    bot_resp,
                    response_time,
                    primary_feedback,
                    secondary_feedback,
                    additional_comments,
                ],
                outputs=[
                    primary_feedback,
                    secondary_feedback,
                    additional_comments
                    ],
            )
    submit_btn.click()(
        fn=get_initial_response,
        inputs=usr_msg,
        outputs=[bot_resp, response_time],
    )

# Launch the interface
feedback_interface.launch()