import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import schedule
import time
import threading

TELEGRAM_BOT_TOKEN = "8179497933:AAFuOlgsAaEvptNK88XRCu6oHgH44lVLdgE"

# Initialize the bot
bot = telegram.Bot(token=TELEGRAM_BOT_TOKEN)

# We'll store chat_ids of users who start using the bot
user_chat_ids = set()

# Example: handle /start command
def start(update, context):
    user_chat_ids.add(update.message.chat_id)
    context.bot.send_message(chat_id=update.message.chat_id,
                             text="Hello! I’m your AI Girlfriend/Boyfriend chatbot. Feel free to chat with me anytime!")

# Example: handle text messages
def echo(update, context):
    user_text = update.message.text
    
    # 1. Send user_text to your hugging face model endpoint for generation
    #    or call your internal function if the model is loaded in the same code.
    
    # For simplicity, let's just do a placeholder response:
    response = "Your AI GF/BF says: " + user_text[::-1]  # example reversed text
    context.bot.send_message(chat_id=update.message.chat_id, text=response)

# Set up the Updater and Dispatcher
updater = Updater(TELEGRAM_BOT_TOKEN, use_context=True)
dispatcher = updater.dispatcher

# Handlers
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))

# Let's define a function that sends proactive messages
def send_proactive_messages():
    for chat_id in user_chat_ids:
        bot.send_message(chat_id=chat_id, text="Hey! Hope you’re doing well. Have you had your lunch yet?")

# We’ll schedule a daily message at e.g. 13:00 (1 PM)
schedule.every().day.at("13:00").do(send_proactive_messages)

def run_scheduler():
    while True:
        schedule.run_pending()
        time.sleep(1)

# We can run the bot + scheduler in separate threads
def run_telegram_bot():
    updater.start_polling()
    updater.idle()

# In your main
if __name__ == "__main__":
    # Start the scheduling thread
    threading.Thread(target=run_scheduler, daemon=True).start()
    
    # Start Telegram bot
    run_telegram_bot()