import re
import pandas as pd
import gradio as gr
import ast
import random
from main import mg_debug_demo
from groq import Groq
from openai import OpenAI


def debug_code(key, model, code, fixed_code, test_cases):
    if model == 'deepseek-coder':
        client = OpenAI(
            api_key=key,
            base_url="https://api.deepseek.com"
        )
    elif model in ['gemma2-9b-it']:
        client = Groq(
            api_key=key,
        )
    else:
        client = OpenAI(
            api_key=key
        )
    fixed_code = mg_debug_demo(code, test_cases, given_model=model, given_client=client)
    return code, fixed_code, test_cases


app = gr.Blocks(
    theme=gr.themes.Default(primary_hue="red", secondary_hue="pink", neutral_hue="gray")
)

with app:
    with gr.Row():
        gr.Markdown("# MGDebugger Demo")
    with gr.Row():
        with gr.Column():
            with gr.Row():
                openai_key_input = gr.Textbox(
                    label="API Key",
                    placeholder="Enter your API key here",
                    type="password",
                )
                model_selector = gr.Dropdown(
                    label="Choose Model",
                    choices=[("gemma2. Please get your api key from https://console.groq.com/keys", "gemma2-9b-it"),
                             ("deepseek-coder. Please get your api key from https://platform.deepseek.com/api-docs/", "deepseek-coder"), 
                             ("gpt-4o-mini. Please get your api key from https://platform.openai.com/settings/profile?tab=api-keys", "gpt-4o-mini"),
                             ("gpt-4-1106-preview. Please get your api key from https://platform.openai.com/settings/profile?tab=api-keys", "gpt-4-1106-preview")],
                    value="deepseek-coder",
                )
            code_input = gr.TextArea(
                label="Code Input",
                placeholder="Enter your code here",
                lines=10,
                )
            test_cases = gr.TextArea(
                label="Public Test Cases",
                placeholder="Enter your public test cases",
                lines=10,
            )
            with gr.Row():  # This Row will contain the buttons
                debug_button = gr.Button("Debug", variant="primary")
                clear_button = gr.Button("Clear", variant="neutral")
        with gr.Column():
            fixed_code_output = gr.TextArea(
                label="Fixed Code",
                placeholder="Fixed code will be shown here",
                lines=10,
                interactive=False,
                visible=True,
            )
    debug_button.click(
        debug_code,
        inputs=[
            openai_key_input,
            model_selector,
            code_input,
            fixed_code_output,
            test_cases,
        ],
        outputs=[ code_input, fixed_code_output, test_cases],
    )

    def clear_inputs():
        return (
            "",
            "",
            pd.DataFrame(
                {
                    "Pass?": [],
                    "Expression": [],
                    "Expected Value": [],
                    "Actual Value": [],
                }
            ),
            "",
            "",
        )

    clear_button.click(
        clear_inputs,
        inputs=[],
        outputs=[code_input, test_cases, fixed_code_output],
    )

    gr.Markdown("## Text Examples")
    gr.Examples(
        [
            [
                
                {'input': {'music_string': 'o o| .| o| o| .| .| .| .| o o'}, 'expected_output': [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]}
                ,
                '''
def parse_music(music_string: str) -> List[int]:
    """ Input to this function is a string representing musical notes in a special ASCII format.
    Your task is to parse this string and return list of integers corresponding to how many beats does each
    not last.

    Here is a legend:
    'o' - whole note, lasts four beats
    'o|' - half note, lasts two beats
    '.|' - quater note, lasts one beat

    >>> parse_music('o o| .| o| o| .| .| .| .| o o')
    [4, 2, 1, 2, 2, 1, 1, 1, 1, 4, 4]
    """
    note_map = {'o': 3, 'o|': 2, '.|': 1}
    return [note_map[x] for x in music_string.split(' ') if x]
    ''',
            ],
            [
                {'input': {'numbers': [1.0, 2.0, 3.0], 'threshold': 0.5}, 'expected_output': False},
                '''
def has_close_elements(numbers: List[float], threshold: float) -> bool:
    """ Check if in given list of numbers, are any two numbers closer to each other than
    given threshold.
    >>> has_close_elements([1.0, 2.0, 3.0], 0.5)
    False
    >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)
    True
    """
    for idx, elem in enumerate(numbers):
        for idx2, elem2 in enumerate(numbers):
            if idx != idx2:
                distance = elem - elem2
                if distance < threshold:
                    return True

    return False
    '''
            ],
            [
                {'input': {'operations': [1, 2, -4, 5]}, 'expected_output': False},
                '''
def below_zero(operations: List[int]) -> bool:
    """ You're given a list of deposit and withdrawal operations on a bank account that starts with
    zero balance. Your task is to detect if at any point the balance of account fallls below zero, and
    at that point function should return True. Otherwise it should return False.
    >>> below_zero([1, 2, 3])
    False
    >>> below_zero([1, 2, -4, 5])
    True
    """
    balance = 0

    for op in operations:
        balance += op
        if balance == 0:
            return True

    return False
    '''
            ]
        ],
        inputs=[test_cases, code_input],
    )


app.launch()