import logging
import asyncio
from aiogram import Bot, Dispatcher
from config import BOT_TOKEN
from db import db

# Import routers
from handlers import router as handlers_router
from tasks import background_tasks

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

async def on_startup(bot: Bot):
    logger.info("Bot starting up...")
    
    # Initialize database
    await db.connect()
    
    # Start background tasks
    asyncio.create_task(background_tasks.daily_limit_reset_scheduler(bot))
    asyncio.create_task(background_tasks.puan2mass_queue_processor(bot))

async def main():
    # Initialize bot and dispatcher
    bot = Bot(token=BOT_TOKEN)
    dp = Dispatcher()
    
    # Register routers
    dp.include_router(handlers_router)
    
    # Register startup handler
    dp.startup.register(on_startup)

    # Start polling
    logger.info("Starting polling...")
    await dp.start_polling(bot)

if __name__ == '__main__':
    try:
        asyncio.run(main())
    except (KeyboardInterrupt, SystemExit):
        logger.info("Bot stopped.")
