@echo off
:: Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

:: If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
    pushd "%CD%"
    CD /D "%~dp0"

echo ========================================================
echo Adding Firewall Rule for Django Dev Server & WeChat Debug (Ports 8000-9000)
echo ========================================================

netsh advfirewall firewall show rule name="DjangoDevServer" >nul
if %errorlevel% equ 0 (
    echo Rule "DjangoDevServer" already exists. Deleting old rule...
    netsh advfirewall firewall delete rule name="DjangoDevServer"
)

:: Add rule for TCP ports 8000-9000, allowing connection from any profile (Domain, Private, Public)
netsh advfirewall firewall add rule name="DjangoDevServer" dir=in action=allow protocol=TCP localport=8000-9000 profile=any
if %errorlevel% equ 0 (
    echo [SUCCESS] Firewall rule added successfully. Ports 8000-9000 are now open on ALL network profiles.
) else (
    echo [ERROR] Failed to add firewall rule.
)

echo.
echo ========================================================
echo Network Configuration Info:
echo ========================================================
ipconfig | findstr "IPv4"
echo.
echo IMPORTANT:
echo 1. Ensure your phone is connected to the SAME Wi-Fi as this computer.
echo 2. Do NOT use mobile data (4G/5G).
echo 3. If you have 3rd party antivirus (360, McAfee, etc.), you may need to manually allow port 8000.
echo.
pause
