# Script push code lên GitHub với token
cd E:\opennotion

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "  Đang push code lên GitHub..." -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# Kiểm tra remote
Write-Host "[1/5] Kiểm tra remote..." -ForegroundColor Yellow
git remote -v
Write-Host ""

# Add files
Write-Host "[2/5] Thêm tất cả file vào staging..." -ForegroundColor Yellow
git add .
$staged = git diff --cached --name-only
if ($staged.Count -gt 0) {
    Write-Host "✓ Đã thêm $($staged.Count) file(s)" -ForegroundColor Green
} else {
    Write-Host "⚠ Không có file mới để thêm" -ForegroundColor Yellow
}
Write-Host ""

# Commit
Write-Host "[3/5] Tạo commit..." -ForegroundColor Yellow
$hasChanges = git diff --cached --quiet
if ($LASTEXITCODE -ne 0) {
    $commitMsg = "Update: OpenNotion project - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
    git commit -m $commitMsg
    Write-Host "✓ Đã commit: $commitMsg" -ForegroundColor Green
} else {
    Write-Host "⚠ Không có thay đổi để commit" -ForegroundColor Yellow
    # Tạo empty commit để đảm bảo có gì đó để push
    git commit --allow-empty -m "Update: OpenNotion project - $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
    Write-Host "✓ Đã tạo empty commit" -ForegroundColor Green
}
Write-Host ""

# Đảm bảo branch là main
Write-Host "[4/5] Đảm bảo branch là main..." -ForegroundColor Yellow
git branch -M main
Write-Host "✓ Branch: main" -ForegroundColor Green
Write-Host ""

# Push
Write-Host "[5/5] Push lên GitHub..." -ForegroundColor Yellow
Write-Host "Repository: https://github.com/Hoanganhvu123/cuccu_note" -ForegroundColor Cyan
Write-Host ""

$pushOutput = git push -u origin main 2>&1
$pushExitCode = $LASTEXITCODE

Write-Host $pushOutput

if ($pushExitCode -eq 0) {
    Write-Host ""
    Write-Host "========================================" -ForegroundColor Green
    Write-Host "  ✓ THÀNH CÔNG! Code đã được push!" -ForegroundColor Green
    Write-Host "========================================" -ForegroundColor Green
    Write-Host ""
    Write-Host "Xem code tại:" -ForegroundColor Cyan
    Write-Host "https://github.com/Hoanganhvu123/cuccu_note" -ForegroundColor White
    Write-Host ""
    
    # Kiểm tra remote branch
    Write-Host "Kiểm tra remote branch..." -ForegroundColor Yellow
    $remoteInfo = git ls-remote --heads origin main 2>&1
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✓ Remote branch tồn tại" -ForegroundColor Green
        Write-Host $remoteInfo
    }
} else {
    Write-Host ""
    Write-Host "========================================" -ForegroundColor Red
    Write-Host "  ✗ Push thất bại! Exit code: $pushExitCode" -ForegroundColor Red
    Write-Host "========================================" -ForegroundColor Red
    Write-Host ""
    Write-Host "Lỗi chi tiết:" -ForegroundColor Yellow
    Write-Host $pushOutput -ForegroundColor Red
}

