$ErrorActionPreference = "Stop"

$gitPath = ""
$commonPaths = @(
    "C:\Program Files\Git\bin\git.exe",
    "C:\Program Files (x86)\Git\bin\git.exe",
    "$env:LocalAppData\Programs\Git\bin\git.exe",
    "D:\Git\bin\git.exe",
    "D:\Program Files\Git\bin\git.exe"
)

foreach ($path in $commonPaths) {
    if (Test-Path $path) {
        $gitPath = $path
        break
    }
}

if (-not $gitPath) {
    try {
        $gitPath = Get-Command git -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source
    } catch {}
}

if (-not $gitPath) {
    Write-Host "Error: Git not found. Please run this in your local terminal where Git is installed." -ForegroundColor Red
    exit 1
}

Write-Host "Found Git: $gitPath" -ForegroundColor Green

try {
    Write-Host "--- Syncing started ---" -ForegroundColor Yellow
    
    & $gitPath add -A
    
    $date = Get-Date -Format "yyyy-MM-dd HH:mm"
    $message = "feat: UI and logic updates ($date)"
    & $gitPath commit -m $message | Out-Null
    
    Write-Host "Pushing to remote..." -ForegroundColor Yellow
    & $gitPath push
    
    Write-Host "--- Sync successful! ---" -ForegroundColor Green
} catch {
    Write-Host "Error during sync: $($_.Exception.Message)" -ForegroundColor Red
}
