#!/bin/bash
# 🔧 MikroTik Router Configuration - CORRECTED COMMANDS
# =====================================================

echo "🎯 CORRECTED MikroTik CLI Commands for Your Router"
echo "=================================================="
echo ""
echo "Router: Kitonga-WiFi-Router (hAP lite, RouterOS 6.49.19)"
echo ""

echo "⚠️  IMPORTANT: Exit from any sub-menu first!"
echo "============================================="
echo "If you see anything other than [admin@Kitonga-WiFi-Router] >"
echo "Type: quit"
echo "Or press Ctrl+C until you're back to root prompt"
echo ""

echo "✅ STEP 1: Navigate to Root Menu"
echo "--------------------------------"
echo "Make sure your prompt shows:"
echo "[admin@Kitonga-WiFi-Router] >"
echo ""
echo "If it shows something like:"
echo "[admin@Kitonga-WiFi-Router] /system package update>"
echo "Type: /"
echo "or: quit"
echo ""

echo "✅ STEP 2: Check Current Hotspot Profile"
echo "----------------------------------------"
echo "First, let's see what profiles exist:"
echo ""
echo "/ip hotspot user-profile print"
echo ""

echo "✅ STEP 3: Configure External Authentication"
echo "--------------------------------------------"
echo "Copy and paste these commands ONE BY ONE:"
echo ""

echo "# Set login method to cookie only"
echo '/ip hotspot user-profile set default login-by=cookie'
echo ""

echo "# Set authentication URL"
echo '/ip hotspot user-profile set default http-cookie-auth-url="https://api.kitonga.klikcell.com/api/mikrotik/auth/"'
echo ""

echo "# Set logout URL"
echo '/ip hotspot user-profile set default http-cookie-logout-url="https://api.kitonga.klikcell.com/api/mikrotik/logout/"'
echo ""

echo "✅ STEP 4: Verify Configuration"
echo "-------------------------------"
echo "Check if the configuration was applied:"
echo ""
echo "/ip hotspot user-profile print detail"
echo ""

echo "✅ STEP 5: Alternative Method (If above fails)"
echo "----------------------------------------------"
echo "If the direct approach doesn't work, try this step-by-step:"
echo ""
echo "1. Navigate to hotspot user-profile menu:"
echo "/ip hotspot user-profile"
echo ""
echo "2. Set each parameter separately:"
echo "set default login-by=cookie"
echo 'set default http-cookie-auth-url="https://api.kitonga.klikcell.com/api/mikrotik/auth/"'
echo 'set default http-cookie-logout-url="https://api.kitonga.klikcell.com/api/mikrotik/logout/"'
echo ""
echo "3. Print to verify:"
echo "print detail"
echo ""
echo "4. Exit back to root:"
echo "/"
echo ""

echo "🔍 TROUBLESHOOTING"
echo "=================="
echo ""
echo "If you get 'bad command name' errors:"
echo "• Make sure you're at the root prompt [admin@Kitonga-WiFi-Router] >"
echo "• Type '/' to go to root menu"
echo "• Type 'quit' to exit sub-menus"
echo ""
echo "If you get 'expected end of command' errors:"
echo "• Make sure quotes are properly closed"
echo "• Try typing commands in sub-menu instead of root"
echo "• Use the alternative method above"
echo ""

echo "✅ EXPECTED OUTPUT"
echo "=================="
echo "After successful configuration, you should see:"
echo ""
echo "login-by: cookie"
echo "http-cookie-auth-url: https://api.kitonga.klikcell.com/api/mikrotik/auth/"
echo "http-cookie-logout-url: https://api.kitonga.klikcell.com/api/mikrotik/logout/"
echo ""

echo "🎯 QUICK TEST"
echo "============="
echo "After configuration, test with:"
echo ""
echo "1. Connect a device to your WiFi hotspot"
echo "2. Open browser - should redirect to captive portal"
echo "3. Try entering phone number as username"
echo "4. Check Django logs at: https://api.kitonga.klikcell.com/admin/"
echo ""

echo "📞 NEED HELP?"
echo "============="
echo "If you're still getting errors:"
echo "• Share the exact error message"
echo "• Confirm your current prompt location"
echo "• Try the web interface method instead"
echo ""
echo "Web interface: http://192.168.0.173"
echo "Go to: IP > Hotspot > Server Profiles > default"
