f"Validity period: {validity_days} days\n\n" \
                         f"Do you want to proceed?"
        user_data[user_id]['validity_days'] = validity_days
        msg = bot.reply_to(message, confirm_message, reply_markup=markup)
        bot.register_next_step_handler(msg, confirm_generation)
    except ValueError:
        bot.reply_to(message, "Please provide a valid number. Use /genkeys to start over.")
def confirm_generation(message):
    if message.text.lower() not in ['yes', 'no']:
        bot.reply_to(message, "Please select Yes or No. Use /genkeys to start over.", 
                    reply_markup=telebot.types.ReplyKeyboardRemove())
        return
    if message.text.lower() == 'no':
        bot.reply_to(message, "Operation cancelled.", 
                    reply_markup=telebot.types.ReplyKeyboardRemove())
        return
    user_id = message.from_user.id
    if user_id not in user_data:
        bot.reply_to(message, "Session expired. Please use /genkeys to start over.", 
                    reply_markup=telebot.types.ReplyKeyboardRemove())
        return
    try:
        num_keys = user_data[user_id]['num_keys']
        validity_days = user_data[user_id]['validity_days']
        new_keys = generate_keys(num_keys, validity_days)
        try:
            with open('keys.json', 'r') as f:
                existing_keys = json.load(f)
        except (FileNotFoundError, json.JSONDecodeError):
            existing_keys = {}
        existing_keys.update(new_keys)
        with open('keys.json', 'w') as f:
            json.dump(existing_keys, f, indent=4)
        response = f"✅ Successfully generated {num_keys} keys!\n\n"
        response += f"🔑 Generated Keys:\n\n"
        for key in new_keys.keys():
            response += f"{key}\n"
        response += f"\n⏳ Validity: {validity_days} days"
        response += f"\n📁 Keys saved to keys.json"
        response += f"\n\n💡 Use /listkeys to view all keys and their status"
        del user_data[user_id]
        bot.reply_to(message, response, parse_mode='Markdown', 
                    reply_markup=telebot.types.ReplyKeyboardRemove())
    except Exception as e:
        bot.reply_to(message, f"An error occurred: {str(e)}", 
                    reply_markup=telebot.types.ReplyKeyboardRemove())
@bot.message_handler(commands=['list'])
def list_keys_command(message):
    if message.from_user.username != 'anetjaka':
        bot.reply_to(message, "⛔ This command is restricted to admin use only.")
        return
    keys = load_keys()
    users = load_users()
    if not keys:
        bot.reply_to(message, "No keys found in the database.")
        return
    response = "🔑 Keys Status:\n\n"
    for key, info in keys.items():
        expiry_date = datetime.fromisoformat(info['expires_at'])
        is_expired = datetime.now() > expiry_date
        is_redeemed = info.get('redeemed', False)
        redeemed_by = info.get('redeemed_by', 'None')
        status = "🔴 Expired" if is_expired else "🟢 Active"
        if is_redeemed:
            status += " (Redeemed)"
        response += f"Key: {key}\n"
        response += f"Status: {status}\n"
        response += f"Expires: {info['expires_at']}\n"
        response += f"Redeemed: {'Yes' if is_redeemed else 'No'}\n"
        if is_redeemed and redeemed_by in users:
            user_info = users[redeemed_by]
            response += f"Redeemed By: {redeemed_by}\n"