#!/bin/bash
# 🎯 MikroTik Router Manual Configuration Guide
# ================================================

echo "🔧 MikroTik Router External Authentication Setup"
echo "=================================================="
echo ""

echo "Your router details:"
echo "Router IP: 192.168.0.173"
echo "Router Name: Kitonga-WiFi-Router"
echo "Model: hAP lite"
echo "Version: 6.49.19"
echo ""

echo "✅ STEP 1: Connect to Router Web Interface"
echo "-------------------------------------------"
echo "1. Open browser and go to: http://192.168.0.173"
echo "2. Login with username: admin"
echo "3. Enter your admin password"
echo ""

echo "✅ STEP 2: Configure Hotspot Server Profile"
echo "-------------------------------------------"
echo "1. Go to: IP > Hotspot > Server Profiles"
echo "2. Double-click on 'default' profile (or create new one)"
echo "3. In the 'General' tab:"
echo "   - Login By: Check ONLY 'cookie'"
echo "   - Uncheck other options (chap, https, trial)"
echo ""
echo "4. In the 'Authentication' tab:"
echo "   - Use Radius: No"
echo "   - HTTP Cookie Authentication URL:"
echo "     https://api.kitonga.klikcell.com/api/mikrotik/auth/"
echo ""
echo "   - HTTP Cookie Logout URL:"
echo "     https://api.kitonga.klikcell.com/api/mikrotik/logout/"
echo ""
echo "5. Click 'OK' to save"
echo ""

echo "✅ STEP 3: Alternative CLI Configuration"
echo "----------------------------------------"
echo "If you prefer command line, connect via SSH and run:"
echo ""
echo "/ip hotspot user-profile"
echo "set [find name=\"default\"] login-by=cookie"
echo ""
echo "/ip hotspot user-profile"
echo "set [find name=\"default\"] http-cookie-auth-url=\"https://api.kitonga.klikcell.com/api/mikrotik/auth/\""
echo ""
echo "/ip hotspot user-profile"
echo "set [find name=\"default\"] http-cookie-logout-url=\"https://api.kitonga.klikcell.com/api/mikrotik/logout/\""
echo ""

echo "✅ STEP 4: Verify Configuration"
echo "-------------------------------"
echo "Run this command to verify:"
echo "/ip hotspot user-profile print detail"
echo ""
echo "You should see:"
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 "✅ STEP 5: Test the Setup"
echo "-------------------------"
echo "1. Connect a device to your WiFi hotspot"
echo "2. Open browser - should redirect to captive portal"
echo "3. Try login with phone number: 0772236727"
echo "4. Check Django admin at: https://api.kitonga.klikcell.com/admin/"
echo "5. Look for access logs in 'Access logs' section"
echo ""

echo "🔍 TROUBLESHOOTING"
echo "=================="
echo ""
echo "If captive portal doesn't appear:"
echo "• Check hotspot is enabled: /ip hotspot print"
echo "• Verify DNS settings point to router"
echo "• Check firewall rules don't block hotspot"
echo ""
echo "If authentication fails:"
echo "• Test API directly: curl -X POST https://api.kitonga.klikcell.com/api/mikrotik/auth/"
echo "• Check Django logs for errors"
echo "• Verify user has active payment"
echo ""
echo "If users can't access after payment:"
echo "• Check user's 'paid_until' date in Django admin"
echo "• Verify device limits not exceeded"
echo "• Test logout and re-login"
echo ""

echo "📊 MONITORING AND LOGS"
echo "====================="
echo ""
echo "Django Admin Logs:"
echo "• Access logs: https://api.kitonga.klikcell.com/admin/billing/accesslog/"
echo "• Users: https://api.kitonga.klikcell.com/admin/billing/user/"
echo "• Payments: https://api.kitonga.klikcell.com/admin/billing/payment/"
echo ""
echo "MikroTik Logs:"
echo "• System > Logging"
echo "• Topics: hotspot, web-proxy"
echo "• Log level: info or debug"
echo ""

echo "🎯 PRODUCTION CHECKLIST"
echo "======================="
echo ""
echo "Before going live:"
echo "□ External authentication configured"
echo "□ Test user authentication works"
echo "□ Test payment integration"
echo "□ Test device limits"
echo "□ Test logout functionality"
echo "□ Monitor logs for errors"
echo "□ Backup router configuration"
echo "□ Document any custom settings"
echo ""

echo "🎉 SETUP COMPLETE!"
echo "=================="
echo ""
echo "Your MikroTik router is ready for external authentication!"
echo "Users will authenticate through your Django API."
echo "Monitor the system and test thoroughly before production use."
