import discord
import requests
from discord.ext import commands
from discord.ui import Select, View
from random import shuffle
from utils import sql_commands as mydb
from utils.bank_functions import *

class MyView(View):
    def __init__(self, right: str, wrong: list):
        super().__init__()
        self.right = right
        options = wrong.copy()
        options.append(right)
        shuffle(options)

        select = Select(
            placeholder="Pick an answer:",
            min_values=1,
            max_values=1,
            options=[discord.SelectOption(label=option) for option in options]
        )

        self.add_item(select)
    
        select.callback = self.select_callback  # Set the callback method

    async def select_callback(self, interaction: discord.Interaction):
        if interaction.data['values'][0] == self.right: # type: ignore
            await interaction.response.edit_message(content=f"You are correct!\n{self.right} was the right answer.", view=None)
        else:
            await interaction.response.edit_message(content=f"Oh no, that's not right!\n{self.right} was the right answer.", view=None)
        
class Test(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.is_owner()
    @commands.command(name="quiz")
    async def quiz(self, ctx):
        try:
            resp = requests.get(
                "https://opentdb.com/api.php?amount=1&category=18&difficulty=medium&type=multiple",
                timeout=10,
            )
            resp.raise_for_status()
            response = resp.json()["results"][0]
        except requests.RequestException:
            await ctx.send("Failed to fetch quiz question.")
            return
        question = response["question"]
        
        view = MyView(response["correct_answer"], response["incorrect_answers"])
        await ctx.send(question, view=view)
        
    @commands.is_owner()
    @commands.command(name="lottery", aliases=["bet" , "lotto"])
    async def lottery(self, ctx, amount:int=None): #type: ignore
        if amount == None:
            await ctx.send(mydb.select("JACKPOT", "global", True, "ID", "1"))
            return
        # mydb.select()
        

async def setup(client):
    await client.add_cog(Test(client))
