import asyncio
from aiogram import F, Router, Bot
from aiogram.filters import CommandStart, Command, CommandObject
from aiogram.types import Message, CallbackQuery 
from aiogram.enums import ParseMode
from aiogram.fsm.state import State, StatesGroup
from aiogram.fsm.context import FSMContext
import app.keyboard as kb
import app.debug_kb as debug_kb
import sqlite3
import app.share
from random import randint
from config import ADMIN_ID, TOKEN

router = Router()

@router.message(CommandStart())
async def cmd_start(message: Message):
    base = sqlite3.connect('users.db')

    cur = base.cursor()

    insert_user = """INSERT OR IGNORE INTO users (first_name, last_name, username, id, is_bot, is_premium)
    VALUES (?, ?, ?, ?, ?, ?)"""

    val = (message.from_user.first_name, message.from_user.last_name, message.from_user.username,
           message.from_user.id, message.from_user.is_bot, message.from_user.is_premium)

    cur.execute(insert_user, val)

    cur.execute("SELECT * FROM users")
    #print(cur.fetchall())
    base.commit()

    base.close()

    await message.answer("тестовый ответ")

@router.message(Command("echo"))
async def echo(message: Message):
    """Повторяет сообщение пользователя."""
    await message.reply(message.text)

# @router.message(Command("test"))
# async def echo(message: Message):
    
#     await message.answer("")

#     await message.reply(message.text)

@router.message(Command("shareold"))
async def share(message: Message, bot: Bot):
    if message.from_user.id == ADMIN_ID:
        base = sqlite3.connect('users.db')

        cur = base.cursor()

        cur.execute("SELECT id FROM users")

        users_id = cur.fetchall()

        for users in users_id:
            user = users[0]
            await bot.send_message(user, 'тестовая рассылка')
            print(user)

        base.commit()

        base.close()
    else:
        await message.answer("вы не администратор")

