import asyncio
import logging
from aiogram import Bot, Dispatcher
import core.apsched
import cfg, base
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from datetime import datetime
import core.service
from dbase.db import DataBase
import core
from core.apsched import send_db_cron
from logger import get_logger

LOG = get_logger()

LOG.info("========= PROGRAM STARTED ==========")

db = DataBase(file=cfg.DATABASE_URL)
dp = Dispatcher()
bot = Bot(token=cfg.TOKEN)


async def main():
    await bot.delete_webhook(drop_pending_updates=True)

    dp.include_routers(base.router, core.service.router)
    logging.basicConfig(level=logging.INFO)

    scheduler = AsyncIOScheduler(timezone='Europe/Moscow')
    scheduler.add_job(send_db_cron, trigger='cron', hour=0, minute=0, start_date=datetime.now(), kwargs={'bot':bot, 'ADMIN_LIST': cfg.ADMIN_LIST})
    scheduler.start()
    
    try:
        await dp.start_polling(bot)
    except Exception as e:
        print("ERROR : Failed to fetch updates!\n")
        print(e)


def run():
    asyncio.run(main())


if __name__ == "__main__":
    run()

