import asyncio
from pathlib import Path
import configparser
import os

os.environ["OPENBLAS_NUM_THREADS"] = "1"
os.environ["NUMEXPR_NUM_THREADS"] = "1"
os.environ["OMP_NUM_THREADS"] = "1"

import pandas as pd
import random
from telegram import Bot

CONFIG_NAME = "config.ini"
CONFIG_FOLDER = "config"
PROJECT_DIRECTORY = Path(__file__).parent.parent
# -------------------------------------------------- #
# READ CONFIG FILE
# Read main config file
config = configparser.ConfigParser()
config_path = Path(PROJECT_DIRECTORY) / "config" / CONFIG_NAME
config.read(config_path)
config_default = config["DEFAULT"]

# -------------------------------------------------- #

bot_token = config_default["Bot_Token"]
# chat_id = get_chat_id(bot_token)
chat_id = config_default["Chat_ID"]
quotes_file_path = os.path.join(PROJECT_DIRECTORY, 'data', 'quotes.csv')

bot = Bot(token=bot_token)

async def send_random_quote():
    """
    Asynchronously sends a random quote from a CSV file to a Telegram bot.

    This function loads quotes from a CSV file specified by the `quotes_file_path` variable. 
    It then randomly selects a row from the DataFrame and retrieves the values for the 'Title' and 'Quote' columns. 
    The selected quote is then formatted into an HTML-formatted message. 
    Finally, the message is sent to a Telegram bot using the `bot.send_message()` method, with the `chat_id` and `text` parameters set accordingly.

    Parameters:
    None

    Returns:
    None
    """
    # Load quotes from the CSV file
    df = pd.read_csv(quotes_file_path, sep=';')
    
    # Use SystemRandom to generate a random index
    sys_random = random.SystemRandom()
    random_index = sys_random.randint(0, len(df) - 1)
    random_row = df.iloc[random_index]

    # Extract the title and quote
    title = random_row['Title']
    quote = random_row['Quote']
    
    # Format the message
    message = f"<b>{title}</b>\n\n{quote}"
    
    # Send the quote via Telegram bot
    await bot.send_message(chat_id=chat_id, text=message, parse_mode='HTML')
        
def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(send_random_quote())

if __name__ == "__main__":
    main()
