@echo off
title Bidda School Management - WordPress Auto Setup

echo =========================================
echo Bidda School Management - WordPress Auto Setup
echo =========================================
echo.

REM Check if running on Windows
if "%OS%"=="Windows_NT" goto WINNT
echo This script is designed for Windows systems only.
goto END

:WINNT
echo Checking system requirements...
echo.

REM Check if XAMPP is installed
if exist "D:\xampp\xampp-control.exe" (
    set XAMPP_PATH=D:\xampp
    echo ✓ XAMPP found at: %XAMPP_PATH%
) else if exist "C:\xampp\xampp-control.exe" (
    set XAMPP_PATH=C:\xampp
    echo ✓ XAMPP found at: %XAMPP_PATH%
) else (
    echo ✗ XAMPP not found
    echo Please install XAMPP first from https://www.apachefriends.org/download.html
    echo.
    echo Press any key to open the download page...
    pause >nul
    start "" "https://www.apachefriends.org/download.html"
    goto END
)

REM Start XAMPP services
echo.
echo Starting XAMPP services...
echo Please wait for Apache and MySQL to start in the XAMPP Control Panel
echo.
start "" "%XAMPP_PATH%\xampp-control.exe"
echo Press any key after starting Apache and MySQL services...
pause >nul

REM Wait for services to start
echo Waiting for services to initialize...
timeout /t 10 /nobreak >nul

REM Download WordPress
echo.
echo Downloading WordPress...
if not exist "%XAMPP_PATH%\htdocs\wordpress-latest.zip" (
    echo Downloading latest WordPress version...
    powershell -Command "Invoke-WebRequest https://wordpress.org/latest.zip -OutFile '%XAMPP_PATH%\htdocs\wordpress-latest.zip'"
    if %errorlevel% == 0 (
        echo ✓ WordPress downloaded successfully
    ) else (
        echo ✗ Failed to download WordPress
        echo Please download manually from https://wordpress.org/latest.zip
        goto END
    )
) else (
    echo ✓ WordPress archive already exists
)

REM Extract WordPress
echo.
echo Extracting WordPress...
if not exist "%XAMPP_PATH%\htdocs\wordpress" (
    powershell -Command "Expand-Archive -Path '%XAMPP_PATH%\htdocs\wordpress-latest.zip' -DestinationPath '%XAMPP_PATH%\htdocs'"
    if %errorlevel% == 0 (
        echo ✓ WordPress extracted successfully
    ) else (
        echo ✗ Failed to extract WordPress
        echo Please extract wordpress-latest.zip manually
        goto END
    )
) else (
    echo ✓ WordPress already extracted
)

REM Create WordPress database
echo.
echo Creating WordPress database...
mysql -u root -e "CREATE DATABASE IF NOT EXISTS wordpress;" 2>nul
if %errorlevel% == 0 (
    echo ✓ WordPress database created successfully
) else (
    echo ⚠ Could not create database automatically
    echo Please create the database manually:
    echo 1. Open http://localhost/phpmyadmin
    echo 2. Click "New" 
    echo 3. Enter database name: wordpress
    echo 4. Click "Create"
    echo.
    echo Press any key after creating the database...
    pause >nul
)

REM Configure WordPress
echo.
echo Configuring WordPress...
if exist "%XAMPP_PATH%\htdocs\wordpress\wp-config-sample.php" (
    copy "%XAMPP_PATH%\htdocs\wordpress\wp-config-sample.php" "%XAMPP_PATH%\htdocs\wordpress\wp-config.php" >nul
    powershell -Command "(Get-Content '%XAMPP_PATH%\htdocs\wordpress\wp-config.php') -replace 'database_name_here', 'wordpress' | Set-Content '%XAMPP_PATH%\htdocs\wordpress\wp-config.php'"
    powershell -Command "(Get-Content '%XAMPP_PATH%\htdocs\wordpress\wp-config.php') -replace 'username_here', 'root' | Set-Content '%XAMPP_PATH%\htdocs\wordpress\wp-config.php'"
    powershell -Command "(Get-Content '%XAMPP_PATH%\htdocs\wordpress\wp-config.php') -replace 'password_here', '' | Set-Content '%XAMPP_PATH%\htdocs\wordpress\wp-config.php'"
    echo ✓ WordPress configured successfully
) else (
    echo ⚠ WordPress config file not found
)

REM Install Bidda plugin
echo.
echo Installing Bidda School Management plugin...
set PLUGIN_PATH=%XAMPP_PATH%\htdocs\wordpress\wp-content\plugins\bidda-school-management
if not exist "%PLUGIN_PATH%" mkdir "%PLUGIN_PATH%"

REM Copy main plugin files (if they exist in current directory)
if exist "bidda-school-management.php" (
    copy "*.php" "%PLUGIN_PATH%\" >nul
    copy "*.md" "%PLUGIN_PATH%\" >nul
    copy "*.txt" "%PLUGIN_PATH%\" >nul
    echo ✓ Main plugin files copied
)

REM Copy includes directory
if exist "includes\" (
    xcopy /E /I "includes" "%PLUGIN_PATH%\includes" >nul
    echo ✓ Includes directory copied
)

REM Copy main directories
for %%D in (admin fees students teachers classes exams attendance library notices uploads) do (
    if exist "%%D\" (
        xcopy /E /I "%%D" "%PLUGIN_PATH%\%%D" >nul
        echo ✓ %%D directory copied
    )
)

echo.
echo =========================================
echo AUTO SETUP COMPLETED!
echo =========================================
echo.
echo Next steps:
echo 1. Open your browser and go to: http://localhost/wordpress
echo 2. Complete the WordPress installation
echo 3. Log in to WordPress admin
echo 4. Activate the 'Bidda School Management' plugin
echo 5. Configure the plugin settings
echo.
echo Default WordPress login:
echo URL: http://localhost/wordpress/wp-login.php
echo Username: admin
echo Password: admin123 (change after first login)
echo.
echo Bidda integration will be available through shortcodes:
echo - [bidda_login] for login page
echo - [bidda_dashboard] for dashboard
echo - [bidda_my_fees] for fees page
echo.
echo Press any key to exit...
pause >nul

:END