@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
echo 🚀 TSP智能助手 - 快速推送
echo.

:: 检查Git状态
git status --porcelain >nul 2>&1
if %errorlevel% neq 0 (
    echo ❌ Git未初始化或不在Git仓库中
    pause
    exit /b 1
)

:: 检查是否有参数
if "%1"=="" (
    :: 智能生成提交信息
    echo 📝 分析markdown文件并生成提交信息...
    
    :: 检查是否有markdown文件修改
    set md_files=
    for /f "tokens=*" %%f in ('git diff --name-only --cached 2^>nul ^| findstr /i "\.md$"') do (
        set md_files=!md_files! %%f
    )
    for /f "tokens=*" %%f in ('git diff --name-only 2^>nul ^| findstr /i "\.md$"') do (
        set md_files=!md_files! %%f
    )
    
    set commit_msg=
    if not "%md_files%"=="" (
        echo 📄 检测到markdown文件修改: %md_files%
        
        :: 提取markdown文件的主要内容
        set commit_title=
        set commit_type=docs
        
        :: 检查是否有修复相关内容
        for %%f in (%md_files%) do (
            if exist "%%f" (
                for /f "tokens=*" %%l in ('type "%%f" ^| findstr /i "修复\|解决\|问题\|错误"') do (
                    set commit_type=fix
                    set commit_title=修复问题
                    goto :found_fix
                )
            )
        )
        
        :: 检查是否有新功能相关内容
        for %%f in (%md_files%) do (
            if exist "%%f" (
                for /f "tokens=*" %%l in ('type "%%f" ^| findstr /i "功能\|新增\|添加\|实现"') do (
                    set commit_type=feat
                    set commit_title=新增功能
                    goto :found_feature
                )
            )
        )
        
        :: 检查是否有优化相关内容
        for %%f in (%md_files%) do (
            if exist "%%f" (
                for /f "tokens=*" %%l in ('type "%%f" ^| findstr /i "优化\|性能\|改进\|提升"') do (
                    set commit_type=perf
                    set commit_title=性能优化
                    goto :found_optimization
                )
            )
        )
        
        :: 提取文件标题
        for %%f in (%md_files%) do (
            if exist "%%f" (
                for /f "tokens=*" %%l in ('type "%%f" ^| findstr /n "^#" ^| head -1') do (
                    set line=%%l
                    set line=!line:*:=!
                    set line=!line:# =!
                    set line=!line:## =!
                    if "!line!" neq "" (
                        set commit_title=!line!
                        goto :found_title
                    )
                )
            )
        )
        
        :found_fix
        :found_feature
        :found_optimization
        :found_title
        
        if "%commit_title%"=="" (
            set commit_title=更新文档记录
        )
        
        :: 生成提交信息
        set commit_msg=%commit_type%: %commit_title%
    ) else (
        echo ℹ️ 没有检测到markdown文件修改
        set commit_msg=feat: 快速提交 - %date% %time%
    )
) else (
    set commit_msg=%1
)

echo 📝 提交信息: %commit_msg%
echo.

:: 检查是否有更改需要提交（含未跟踪文件）
setlocal enabledelayedexpansion

git diff --quiet
set has_unstaged=%errorlevel%

git diff --cached --quiet
set has_staged=%errorlevel%

set has_untracked=0
for /f "delims=" %%f in ('git ls-files --others --exclude-standard') do set has_untracked=1

if %has_unstaged% equ 0 if %has_staged% equ 0 if %has_untracked% equ 0 (
    echo ℹ️ 没有检测到任何更改，无需提交
    echo.
    echo ✅ 工作区干净，无需推送
    pause
    exit /b 0
)

:: 执行推送
echo.
echo 📤 开始推送流程...
echo 📝 提交信息: %commit_msg%

git add .
if %errorlevel% neq 0 (
    echo ❌ 添加文件失败
    pause
    exit /b 1
)

git commit -m "%commit_msg%"
if %errorlevel% neq 0 (
    echo ❌ 提交失败
    pause
    exit /b 1
)

git fetch origin main
git push origin main
if %errorlevel% equ 0 (
    echo.
    echo ✅ 推送完成！
    echo 📊 最新提交:
    git log --oneline -1
) else (
    echo.
    echo ❌ 推送失败，尝试自动解决...
    echo 🔄 执行: git pull origin main --rebase
    git pull origin main --rebase
    if %errorlevel% equ 0 (
        echo ✅ 重试推送...
        git push origin main
        if %errorlevel% equ 0 (
            echo ✅ 推送成功！
            echo 📊 最新提交:
            git log --oneline -1
        ) else (
            echo ❌ 重试推送失败，请手动处理
        )
    ) else (
        echo ❌ 自动rebase失败，请手动处理冲突后重试
    )
)

echo.
pause
