import discord
from discord.ext import commands
from random import choice
import requests
from dotenv import dotenv_values

# Initialize responses for eightball command
eightball_responses = [
    "Yes, definitely.",
    "No, not at all.",
    "Ask again later.",
    "Outlook not so good.",
    "Most likely.",
    "Don't count on it.",
    "Signs point to yes.",
    "Very doubtful.",
    "Without a doubt.",
    "My sources say no.",
    "Outlook is good.",
    "It is certain.",
    "Cannot predict now.",
    "Better not tell you now.",
    "Yes, in due time.",
    "No, never.",
    "Concentrate and ask again.",
    "Very likely.",
    "My reply is no.",
    "Outlook not good.",
    "Yes, for sure.",
    "Don't hold your breath.",
    "As I see it, yes.",
    "It is decidedly so.",
    "My sources say yes.",
    "Definitely not.",
    "Yes, definitely.",
    "Very unlikely.",
    "Ask again another time.",
    "Cannot foretell now.",
    "The outlook is bleak.",
    "Not in a million years.",
    "It is certain.",
    "Outlook not good.",
    "Signs point to yes.",
    "Without a doubt.",
    "My sources say no.",
    "Yes, in due time.",
    "Definitely not.",
    "Ask again later.",
    "Better not tell you now.",
    "Very likely.",
    "Yes, for sure.",
    "No, not at all.",
    "Don't count on it.",
    "As I see it, yes.",
    "My reply is no.",
    "Concentrate and ask again.",
    "Outlook is good.",
    "It is decidedly so.",
    "My sources say yes.",
    "Very unlikely.",
    "Cannot predict now.",
    "Yes, definitely.",
    "Not in a million years.",
    "Ask again another time.",
    "Without a doubt.",
    "Signs point to yes.",
    "The outlook is bleak.",
    "Definitely not.",
    "Outlook not good.",
    "Yes, for sure.",
    "Better not tell you now.",
    "My sources say no.",
    "It is certain.",
    "Very likely.",
    "Don't count on it.",
    "Concentrate and ask again.",
    "No, not at all.",
    "Ask again later.",
    "Yes, definitely.",
    "Without a doubt.",
    "Outlook is good.",
    "My sources say yes.",
    "Not in a million years.",
    "Very unlikely.",
    "Definitely not.",
    "Better not tell you now.",
    "As I see it, yes.",
    "The outlook is bleak.",
    "Concentrate and ask again.",
    "Yes, for sure.",
    "Signs point to yes.",
    "My reply is no.",
    "Cannot predict now.",
    "Ask again another time.",
    "No, not at all.",
    "Don't count on it.",
    "Outlook not so good.",
    "It is certain.",
    "Without a doubt.",
    "Very likely.",
    "Yes, definitely.",
    "My sources say no.",
    "Better not tell you now.",
    "Definitely not.",
    "Ask again later.",
    "Concentrate and ask again.",
    "The outlook is bleak.",
    "Not in a million years.",
    "Yes, for sure.",
    "As I see it, yes.",
    "Don't count on it.",
    "Very unlikely.",
    "My reply is no.",
    "Ask again another time.",
    "Signs point to yes.",
    "Outlook is good.",
    "Without a doubt.",
    "It is certain.",
    "Yes, definitely.",
    "Definitely not.",
    "Better not tell you now.",
    "Concentrate and ask again.",
    "No, not at all.",
    "My sources say yes.",
    "The outlook is bleak.",
    "Very likely.",
    "Ask again later.",
    "Don't count on it.",
    "As I see it, yes.",
    "Yes, for sure.",
    "Not in a million years.",
    "Cannot predict now.",
    "Outlook not good.",
    "Without a doubt.",
    "My reply is no.",
    "Yes, definitely.",
    "Better not tell you now.",
    "Concentrate and ask again.",
    "Definitely not.",
    "Ask again another time.",
    "Signs point to yes.",
    "It is certain.",
    "Very unlikely.",
    "Outlook is good.",
    "No, not at all.",
    "The outlook is bleak.",
    "As I see it, yes.",
    "My sources say no.",
    "Without a doubt.",
    "Better not tell you now.",
    "Concentrate and ask again.",
    "Yes, for sure.",
    "Not in a million years.",
    "Ask again later.",
    "Definitely not.",
    "My reply is no.",
    "Outlook not so good.",
    "Very likely.",
    "It is certain.",
    "Signs point to yes.",
    "Don't count on it.",
    "Yes, definitely.",
    "Better not tell you now.",
    "Concentrate and ask again.",
    "Without a doubt.",
    "Ask again another time.",
    "Definitely not.",
    "Outlook is good.",
    "No, not at all.",
    "The outlook is bleak.",
    "As I see it, yes.",
    "Very unlikely.",
    "My sources say yes.",
    "Yes, for sure.",
    "Cannot predict now.",
    "Better not tell you now.",
    "Concentrate and ask again.",
    "It is certain.",
    "Ask again later.",
    "Signs point to yes.",
    "Outlook not good.",
    "Without a doubt.",
    "Definitely not.",
    "Don't count on it.",
    "My reply is no.",
    "The outlook is bleak.",
    "Very likely.",
    "Yes, for sure.",
    "As I see it, yes.",
    "No, not at all.",
    "Concentrate and ask again.",
    "Better not tell you now.",
    "Ask again another time.",
    "My sources say yes.",
    "It is certain.",
    "Outlook is good.",
    "Yes, definitely.",
    "Without a doubt.",
    "Definitely not.",
    "Very unlikely.",
    "The outlook is bleak.",
    "Concentrate and ask again.",
    "Signs point to yes.",
    "No, not at all.",
    "Ask again later.",
    "Better not tell you now.",
    "As I see it, yes.",
    "My reply is no.",
    "Yes, for sure.",
]


class Fun(commands.Cog):
    def __init__(self, client):
        self.client = client
        # Load environment variables for API keys
        config = dotenv_values(".env")
        self.tenor_api_key = config.get("TENORAPI")

    @commands.command(
        name="eightball",
        aliases=["eb", "8ball", "8b"],
        brief="Ask a question and I shall answer.",
        description="I will predict the answer to any and all of your questions.",
    )
    async def eight_ball(self, ctx, *, question: str):
        """Respond with a random answer to a yes/no question."""
        if question:
            response = choice(eightball_responses)
            await ctx.send(f"Answer: {response}")
        else:
            await ctx.send("Please ask a question for the 8ball to answer.")

    @commands.command()
    async def randomgif(self, ctx, *query):
        """Fetch and display a random GIF based on a search query."""
        if not self.tenor_api_key:
            await ctx.send("Tenor API key is not configured.")
            return

        search_query = "%20".join(query) if query else "funny"
        api_url = f"https://tenor.googleapis.com/v2/search?q={search_query}&key={self.tenor_api_key}&limit=10"

        try:
            response = requests.get(api_url, timeout=10)
            response.raise_for_status()
            gif_data = response.json()

            if gif_data.get("results"):
                gif_url = choice(gif_data["results"])["media_formats"]["gif"]["url"]
                embed = discord.Embed(title="Here's a random GIF!")
                embed.set_image(url=gif_url)
                await ctx.send(embed=embed)
            else:
                await ctx.send("No GIFs found for your query.")

        except requests.RequestException as error:
            await ctx.send(f"Error fetching Tenor GIF: {error}")

    @commands.command()
    async def meme(self, ctx):
        """Fetch and display a random meme."""
        api_url = "https://meme-api.com/memes/random"

        try:
            response = requests.get(api_url, timeout=10)
            response.raise_for_status()
            meme_data = response.json()

            meme_url = meme_data.get("url")
            if meme_url:
                await ctx.send(meme_url)
            else:
                await ctx.send("Couldn't fetch a meme at the moment.")

        except requests.RequestException:
            await ctx.send("Error fetching meme.")


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