import logging
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.middlewares.logging import LoggingMiddleware
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters import Command
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.types import ReplyKeyboardMarkup, KeyboardButton, InlineKeyboardMarkup, InlineKeyboardButton

API_TOKEN = '7472030348:AAGI53nX-ON-WBmEhd_qBC6EnZsHOqp_2kE'
GROUP_ID = '-1001961537659'

logging.basicConfig(level=logging.INFO)

bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
dp.middleware.setup(LoggingMiddleware())

# Define states
class OrderForm(StatesGroup):
    question_1 = State()
    question_2 = State()
    question_3 = State()
    question_4 = State()
    question_5 = State()
    question_6 = State()

@dp.message_handler(Command("start"))
async def cmd_start(message: types.Message):
    await message.reply("Привет! Чтобы создать заказ, нажми на кнопку 'Создать заказ' внизу.", reply_markup=ReplyKeyboardMarkup(
        resize_keyboard=True).add(KeyboardButton("Создать заказ")))

@dp.message_handler(Command("menu"))
async def show_menu(message: types.Message):
    await message.reply("Выбери действие:", reply_markup=ReplyKeyboardMarkup(
        resize_keyboard=True).add(KeyboardButton("Создать заказ")))

@dp.message_handler(Command("create_order"))
async def cmd_create_order(message: types.Message):
    await OrderForm.question_1.set()
    await message.reply("Ответьте на первый вопрос:")

@dp.message_handler(state=OrderForm.question_1)
async def process_question_1(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['question_1'] = message.text
    await OrderForm.next()
    await message.reply("Ответьте на второй вопрос:")

# Аналогично для остальных вопросов

@dp.message_handler(state=OrderForm.question_6)
async def process_question_6(message: types.Message, state: FSMContext):
    async with state.proxy() as data:
        data['question_6'] = message.text
    await message.reply("Отлично! Ваши ответы:")
    async with state.proxy() as data:
        for key, value in data.items():
            await message.reply(f"{key}: {value}")

    # Предложить отправить или переписать ответы
    keyboard = InlineKeyboardMarkup().add(InlineKeyboardButton("Отправить", callback_data="send_order")).add(InlineKeyboardButton("Переписать", callback_data="rewrite_order"))
    await message.reply("Хотите отправить эти ответы или переписать?", reply_markup=keyboard)

@dp.callback_query_handler(text="send_order", state="*")
async def send_order(callback_query: types.CallbackQuery, state: FSMContext):
    async with state.proxy() as data:
        for key, value in data.items():
            await bot.send_message(callback_query.from_user.id, f"{key}: {value}")

@dp.callback_query_handler(text="rewrite_order", state="*")
async def rewrite_order(callback_query: types.CallbackQuery):
    await cmd_create_order(callback_query.message)

if __name__ == '__main__':
    from aiogram import executor
    executor.start_polling(dp, skip_updates=True)
