# Pagination API Test Script
# Test employee list pagination and total count

$BaseUrl = "http://localhost:8080"
$LoginUrl = "$BaseUrl/login/test"
$ListUrl = "$BaseUrl/dpc/employee/list"

# Login to get token
$loginBody = @{
    username = "admin"
    password = "admin123"
} | ConvertTo-Json

Write-Host "Logging in..." -ForegroundColor Cyan
$loginResponse = Invoke-RestMethod -Uri $LoginUrl -Method Post -Body $loginBody -ContentType "application/json"
$token = $loginResponse.token

Write-Host "Login success!" -ForegroundColor Green
Write-Host ""

$headers = @{
    Authorization = "Bearer $token"
}

# Test function
function Test-Page {
    param(
        [int]$PageNum,
        [int]$PageSize
    )

    Write-Host "========== Testing: pageNum=$PageNum, pageSize=$PageSize ==========" -ForegroundColor Yellow

    $queryParams = @{
        pageNum = $PageNum
        pageSize = $PageSize
    }

    $queryString = ($queryParams.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join '&'
    $url = "$ListUrl?$queryString"

    try {
        $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers

        $rows = $response.rows.Count
        $total = $response.total
        $code = $response.code

        Write-Host "  Response Code: $code" -ForegroundColor Cyan
        Write-Host "  Returned Rows: $rows" -ForegroundColor Cyan
        Write-Host "  Total Records: $total" -ForegroundColor Cyan

        # Calculate expected values
        $expectedTotalPages = [Math]::Ceiling($total / $pageSize)
        $expectedRows = if ($PageNum -lt $expectedTotalPages) { $pageSize } elseif ($PageNum -eq $expectedTotalPages) { $total - ($pageSize * ($PageNum - 1)) } else { 0 }

        Write-Host "  Expected Rows: $expectedRows" -ForegroundColor Cyan
        Write-Host "  Expected Total Pages: $expectedTotalPages" -ForegroundColor Cyan

        # Verify
        $isCorrect = $rows -eq $expectedRows
        if ($isCorrect) {
            Write-Host "  Result: CORRECT" -ForegroundColor Green
        } else {
            Write-Host "  Result: WRONG! Got $rows rows, expected $expectedRows" -ForegroundColor Red
        }

        # Show returned employee IDs
        if ($response.rows -and $response.rows.Count -gt 0) {
            $ids = ($response.rows | ForEach-Object { $_.employeeId }) -join ', '
            Write-Host "  Employee IDs: $ids" -ForegroundColor Gray
        }

        return @{
            Success = ($code -eq 200)
            Rows = $rows
            Total = $total
            ExpectedRows = $expectedRows
            IsCorrect = $isCorrect
            Response = $response
        }
    }
    catch {
        Write-Host "  Error: $_" -ForegroundColor Red
        return @{
            Success = $false
            Error = $_.Exception.Message
        }
    }
}

Write-Host ""
Write-Host "================ Starting Pagination Tests ================" -ForegroundColor Yellow
Write-Host ""

# Test cases
$testCases = @(
    @{ PageNum = 1; PageSize = 10; Description = "Page 1, Size 10" }
    @{ PageNum = 2; PageSize = 10; Description = "Page 2, Size 10" }
    @{ PageNum = 1; PageSize = 5; Description = "Page 1, Size 5" }
    @{ PageNum = 3; PageSize = 5; Description = "Page 3, Size 5" }
    @{ PageNum = 1; PageSize = 20; Description = "Page 1, Size 20" }
    @{ PageNum = 1; PageSize = 3; Description = "Page 1, Size 3" }
)

$results = @()
$allCorrect = $true

foreach ($testCase in $testCases) {
    Write-Host ""
    $result = Test-Page -PageNum $testCase.PageNum -PageSize $testCase.PageSize
    $results += [PSCustomObject]@{
        Test = $testCase.Description
        PageNum = $testCase.PageNum
        PageSize = $testCase.PageSize
        ReturnedRows = $result.Rows
        TotalRecords = $result.Total
        ExpectedRows = $result.ExpectedRows
        Correct = if ($result.IsCorrect) { "PASS" } else { "FAIL" }
    }

    if (-not $result.IsCorrect) {
        $allCorrect = $false
    }

    Start-Sleep -Milliseconds 200
}

# Show summary
Write-Host ""
Write-Host "================ Test Results Summary ================" -ForegroundColor Yellow
Write-Host ""
$results | Format-Table -AutoSize

Write-Host ""
if ($allCorrect) {
    Write-Host "PASS - All tests passed! Pagination is working correctly." -ForegroundColor Green
} else {
    Write-Host "FAIL - Some tests failed! Please check pagination logic." -ForegroundColor Red
}
