import asyncio
import random
import telegram
from datetime import datetime, time, timedelta
from telegram import Bot
from telegram.constants import ParseMode


TOKEN = "8498368132:AAFvLo1ina_snOx_qMcCbbw6K3zk-O6ixJo"
CHAT_ID = "-1002846623634"
PAIRS = ['AED/CNY OTC','AUD/CAD OTC','AUD/CHF OTC','AUD/USD OTC','CAD/JPY OTC','EUR/JPY OTC','EUR/TRY OTC','EUR/USD OTC','GBP/USD OTC','NZD/JPY OTC','OMR/CNY OTC','SAR/CNY OTC','USD/ARS OTC','USD/BDT OTC','USD/BRL OTC','USD/COP OTC','USD/EGP OTC','USD/MXN OTC','USD/THB OTC','TND/USD OTC','AUD/CAD','GBP/PY OTC','USD/IDR OTC','USD/RUB OTC','USD/JPY','AUD/NZD OTC','GBP/JPY','AUD/USD','CAD/JPY','GBP/CHF',
         'Bitcoin ETF OTC','Bitcoin OTC','Dogecoin OTC','Polkadot OTC','Ethereum OTC','Toncoin OTC','Avalanche OTC','Chainlink OTC','TRON OTC','Cardano OTC',
         'Brent Oil OTC','WTI Crude Oil OTC','Silver OTC','Gold OTC','Natural Gas OTC','Palladium spot OTC',
         'Apple OTC','American Express OTC','FACEBOOK INC OTC','VIX OTC','VISA OTC','Cisco OTC','Alibaba OTC','GameStop Corp OTC']
bot = Bot(token=TOKEN)

def get_signal():
    pairs = PAIRS
    minutes = str(random.randint(2, 3)*5)
    actions = ["вверх на "+minutes+" минут", "вниз на "+minutes+" минут"]
    return f"📊 {random.choice(pairs)} — {random.choice(actions)}"

async def send_signal():
    signal = get_signal()
    now = datetime.now().strftime("%H:%M")
    message = f"🕒 {now}\n<b>🤖 СИГНАЛ ОТ ИИ</b>\n{signal}"
    await bot.send_message(chat_id=CHAT_ID, text=message, parse_mode=ParseMode.HTML)

def get_random_times():
    hours = sorted(random.sample(range(12, 24), 5))
    minutes = [random.randint(0, 59) for _ in range(5)]
    return list(zip(hours, minutes))

async def daily_scheduler():
    while True:
        today_signals = get_random_times()
        print(f"🎯 Сигналы на сегодня: {today_signals}")

        for h, m in today_signals:
            target_time = datetime.combine(datetime.today(), time(h, m))
            now = datetime.now()
            wait_seconds = (target_time - now).total_seconds()

            if wait_seconds > 0:
                print(f"⏳ Ждём до {target_time.strftime('%H:%M')} ({wait_seconds / 60:.1f} мин)")
                await asyncio.sleep(wait_seconds)

            await send_signal()

        # 💤 После всех сигналов ждём до следующего дня (00:00)
        tomorrow = datetime.combine(datetime.today() + timedelta(days=1), time(0, 0))
        sleep_until_tomorrow = (tomorrow - datetime.now()).total_seconds()
        print(f"🌙 Все сигналы отправлены. Спим до {tomorrow.strftime('%Y-%m-%d %H:%M')}")
        await asyncio.sleep(sleep_until_tomorrow)

# 🚀 Запуск
async def main():
    await daily_scheduler()

if __name__ == "__main__":
    asyncio.run(main())