import logging
import os

import nextcord  # add this
import openai
from langchain import OpenAI
from langchain.chains.summarize import load_summarize_chain
from langchain.text_splitter import RecursiveCharacterTextSplitter
from nextcord.ext import commands
from pytube import YouTube

logging.basicConfig(
    level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)


def setup(bot: commands.Bot):
    bot.add_cog(SummaryCog(bot)) # please put this on bottom lol


def progress_func(chunk=None, file_handle=None, remaining=None):
    """progress call back function for the Summarize function"""
    logger.info("progressing...")


def complete_func(self, path):
    """complete callback function for the Summarize function"""
    logger.info("complete")
    logger.info(self)
    logger.info(path)


async def download_yt_file(link):
    yt = YouTube(
        link,
        on_progress_callback=progress_func,
        on_complete_callback=complete_func,
        use_oauth=True,
        allow_oauth_cache=True,
    )
    logger.info("Processing:  " + yt.title)
    stream = yt.streams.filter(only_audio=True).last()
    try:
        ytFile = stream.download(os.getenv("SAVE_PATH"))
        logger.info(f"Processing complete. saving to path {ytFile}")
    except Exception as e:
        ytFile = None
        logger.info(f"Error processing {e}")
    return ytFile


class SummaryCog(commands.Cog):
    def __init__(self, bot: commands.Bot) -> None:
        self.bot = bot
        self.is_busy = False
                            # this is the name     # this is the description
    @nextcord.slash_command(name="summary", description="Summarize a video") # remove commands.commands and add nextcord.slash_command
    async def get_summary(self, interaction: nextcord.Interaction, link): # remove ctx and add interaction: nextcord.Interaction
        ytFile = await download_yt_file(link)

# IN THE WHOLE FILE FIX CTX TO INTERACTION, ANY CTX.AUTHOR TO INTERACTION.USER, AND CTX.SEND TO INTERACTION.REPLY (OR INTERACTION.SEND) DEPENDING ON THE CONTEXT
# DONT USE ALL CAPS, JUST FOR SHOWING YOU WHAT TO CHANGE

        audio_file = open(ytFile, "rb") #
        transcript = openai.Audio.transcribe("whisper-1", audio_file)
        logger.info(transcript)
        prompt = f"Write a Title for the transcript that is under 15 words. " \
                 f"Then write: '--Summary--' " \
                 f"Write 'Summary' as a Heading " \
                 f"1. Write a summary of the provided transcript. " \
                 f"Then  write: '--Additional Info--'. " \
                 f"Then return a list of the main points in the provided transcript. " \
                 f"Then return a list of action items. " \
                 f"Then return a list of follow up questions. " \
                 f"Then return a list of potential arguments against the transcript." \
                 f"For each list, return a Heading 2 before writing the list items. " \
                 f"Limit each list item to 200 words, and return no more than 20  points per list. " \
                 f"Transcript: "

        llm = OpenAI(temperature=0, openai_api_key=os.getenv("OPENAI_API_KEY"))
        num_tokens = llm.get_num_tokens(transcript)
        await interaction.send(f"Number of Tokens in transcript: {num_tokens}")
        logger.info(f"Number of Tokens in transcript: {num_tokens}")
        text_splitter = RecursiveCharacterTextSplitter(separators=["\n\n", "\n"], chunk_size=10000, chunk_overlap=500)
        docs = text_splitter.create_documents([prompt, transcript])
        summary_chain = load_summarize_chain(llm=llm, chain_type='map_reduce', verbose=True)
        output = summary_chain.run(docs)

        await interaction.send(output)

        return output


def setup(bot: commands.Bot):
    bot.add_cog(SummaryCog(bot))
langchain.chains.summarize.load_summarize_chain langchain.text_splitter.RecursiveCharacterTextSplitter
[((286, 393), 'logging.basicConfig', 'logging.basicConfig', ([], {'level': 'logging.INFO', 'format': '"""%(asctime)s - %(name)s - %(levelname)s - %(message)s"""'}), "(level=logging.INFO, format=\n    '%(asctime)s - %(name)s - %(levelname)s - %(message)s')\n", (305, 393), False, 'import logging\n'), ((404, 431), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (421, 431), False, 'import logging\n'), ((909, 1039), 'pytube.YouTube', 'YouTube', (['link'], {'on_progress_callback': 'progress_func', 'on_complete_callback': 'complete_func', 'use_oauth': '(True)', 'allow_oauth_cache': '(True)'}), '(link, on_progress_callback=progress_func, on_complete_callback=\n    complete_func, use_oauth=True, allow_oauth_cache=True)\n', (916, 1039), False, 'from pytube import YouTube\n'), ((1647, 1718), 'nextcord.slash_command', 'nextcord.slash_command', ([], {'name': '"""summary"""', 'description': '"""Summarize a video"""'}), "(name='summary', description='Summarize a video')\n", (1669, 1718), False, 'import nextcord\n'), ((2235, 2283), 'openai.Audio.transcribe', 'openai.Audio.transcribe', (['"""whisper-1"""', 'audio_file'], {}), "('whisper-1', audio_file)\n", (2258, 2283), False, 'import openai\n'), ((3458, 3556), 'langchain.text_splitter.RecursiveCharacterTextSplitter', 'RecursiveCharacterTextSplitter', ([], {'separators': "['\\n\\n', '\\n']", 'chunk_size': '(10000)', 'chunk_overlap': '(500)'}), "(separators=['\\n\\n', '\\n'], chunk_size=10000,\n    chunk_overlap=500)\n", (3488, 3556), False, 'from langchain.text_splitter import RecursiveCharacterTextSplitter\n'), ((3645, 3713), 'langchain.chains.summarize.load_summarize_chain', 'load_summarize_chain', ([], {'llm': 'llm', 'chain_type': '"""map_reduce"""', 'verbose': '(True)'}), "(llm=llm, chain_type='map_reduce', verbose=True)\n", (3665, 3713), False, 'from langchain.chains.summarize import load_summarize_chain\n'), ((1223, 1245), 'os.getenv', 'os.getenv', (['"""SAVE_PATH"""'], {}), "('SAVE_PATH')\n", (1232, 1245), False, 'import os\n'), ((3204, 3231), 'os.getenv', 'os.getenv', (['"""OPENAI_API_KEY"""'], {}), "('OPENAI_API_KEY')\n", (3213, 3231), False, 'import os\n')]