#Requires -Modules Pester
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: MIT

BeforeAll {
    . (Join-Path -Path $PSScriptRoot -ChildPath '../scripts/generate.ps1')
}

Describe 'Test-GitAvailability' {
    It 'Does not throw when git is available' {
        # This test assumes git is installed in the test environment
        { Test-GitAvailability } | Should -Not -Throw
    }

    It 'Should throw when git is not available' {
        Mock Get-Command { $null } -ParameterFilter { $Name -eq 'git' }
        { Test-GitAvailability } | Should -Throw '*Git is required*'
    }
}

Describe 'New-PrDirectory' {
    BeforeAll {
        $script:tempRepo = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
        New-Item -ItemType Directory -Path $script:tempRepo -Force | Out-Null
    }

    AfterAll {
        Remove-Item -Path $script:tempRepo -Recurse -Force -ErrorAction SilentlyContinue
    }

    It 'Creates parent directory for the output file' {
        $outputFile = Join-Path $script:tempRepo '.copilot-tracking/pr/pr-reference.xml'
        $result = New-PrDirectory -OutputFilePath $outputFile
        $result | Should -Not -BeNullOrEmpty
        Test-Path -Path $result -PathType Container | Should -BeTrue
        $result | Should -Match '\.copilot-tracking[\\/]pr$'
    }

    It 'Returns existing directory without error' {
        $outputFile = Join-Path $script:tempRepo '.copilot-tracking/pr/pr-reference.xml'
        $firstCall = New-PrDirectory -OutputFilePath $outputFile
        $secondCall = New-PrDirectory -OutputFilePath $outputFile
        $secondCall | Should -Be $firstCall
    }
}

Describe 'Resolve-ComparisonReference' {
    It 'Returns PSCustomObject with Ref and Label properties' {
        $result = Resolve-ComparisonReference -BaseBranch 'main'
        $result | Should -BeOfType [PSCustomObject]
        $result.PSObject.Properties.Name | Should -Contain 'Ref'
        $result.PSObject.Properties.Name | Should -Contain 'Label'
    }

    It 'Uses merge-base when remote branch exists' {
        # This test assumes main branch exists
        $result = Resolve-ComparisonReference -BaseBranch 'main'
        $result.Ref | Should -Not -BeNullOrEmpty
    }

    It 'Should throw when base branch does not exist' {
        Mock git { $global:LASTEXITCODE = 1; return $null }
        { Resolve-ComparisonReference -BaseBranch 'nonexistent-branch-xyz' } | Should -Throw '*does not exist*'
    }

    Context 'UseMergeBase switch' {
        It 'Resolves merge-base commit when UseMergeBase is set' {
            $result = Resolve-ComparisonReference -BaseBranch 'HEAD~3' -UseMergeBase
            $result.Ref | Should -Not -BeNullOrEmpty
            # merge-base of HEAD and HEAD~3 should be HEAD~3 itself (or its SHA)
            $result.Ref | Should -Match '^[a-f0-9]+'
        }

        It 'Falls back to direct ref when merge-base fails' {
            $script:callCount = 0
            Mock git {
                $script:callCount++
                if ($script:callCount -le 2) {
                    # First calls: rev-parse --verify succeeds
                    $global:LASTEXITCODE = 0
                    return 'abc1234'
                }
                # merge-base call fails
                $global:LASTEXITCODE = 1
                return $null
            }
            $result = Resolve-ComparisonReference -BaseBranch 'some-branch' -UseMergeBase
            $result.Ref | Should -Not -BeNullOrEmpty
        }

        It 'Returns direct ref when UseMergeBase is not set' {
            $result = Resolve-ComparisonReference -BaseBranch 'main'
            # Without merge-base, ref should be the branch name or origin/branch
            $result.Ref | Should -Match '(origin/)?main'
        }
    }
}

Describe 'Get-ShortCommitHash' {
    It 'Returns 7-character hash for HEAD' {
        $result = Get-ShortCommitHash -Ref 'HEAD'
        $result | Should -Match '^[a-f0-9]{7,}$'
    }

    It 'Returns consistent result for same ref' {
        $first = Get-ShortCommitHash -Ref 'HEAD'
        $second = Get-ShortCommitHash -Ref 'HEAD'
        $first | Should -Be $second
    }

    It 'Should throw when ref resolution fails' {
        Mock git { $global:LASTEXITCODE = 128; return '' }
        { Get-ShortCommitHash -Ref 'invalid-ref-xyz' } | Should -Throw "*Failed to resolve ref*"
    }
}

Describe 'Get-CommitEntry' {
    It 'Returns array of formatted commit entries' {
        $result = Get-CommitEntry -ComparisonRef 'HEAD~1'
        $result | Should -BeOfType [string]
    }

    It 'Returns empty array when no commits in range' {
        $result = Get-CommitEntry -ComparisonRef 'HEAD'
        $result | Should -BeNullOrEmpty
    }

    It 'Should throw when commit history retrieval fails' {
        Mock git { $global:LASTEXITCODE = 128; return $null }
        { Get-CommitEntry -ComparisonRef 'main' } | Should -Throw '*Failed to retrieve commit history*'
    }
}

Describe 'Get-CommitCount' {
    It 'Returns integer count' {
        $result = Get-CommitCount -ComparisonRef 'HEAD~5'
        $result | Should -BeOfType [int]
        # Merge commits can inflate the count, so just verify it returns a positive integer
        $result | Should -BeGreaterOrEqual 1
    }

    It 'Returns 0 when no commits in range' {
        $result = Get-CommitCount -ComparisonRef 'HEAD'
        $result | Should -Be 0
    }

    It 'Should throw when commit count fails' {
        Mock git { $global:LASTEXITCODE = 128; return '' }
        { Get-CommitCount -ComparisonRef 'main' } | Should -Throw '*Failed to count commits*'
    }

    It 'Should return 0 when commit count text is empty' {
        Mock git { $global:LASTEXITCODE = 0; return '' }
        $result = Get-CommitCount -ComparisonRef 'main'
        $result | Should -Be 0
    }
}

Describe 'Get-DiffOutput' {
    It 'Returns array of diff lines' {
        $result = Get-DiffOutput -ComparisonRef 'HEAD~1'
        $result | Should -Not -BeNullOrEmpty
    }

    It 'Excludes markdown when specified' {
        # Verify the function executes without error when excluding markdown
        # The result may be empty if only markdown files were changed
        { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludeMarkdownDiff } | Should -Not -Throw
    }

    It 'Should throw when diff output fails' {
        Mock git { $global:LASTEXITCODE = 128; return $null }
        { Get-DiffOutput -ComparisonRef 'main' } | Should -Throw '*Failed to retrieve diff output*'
    }

    Context 'ExcludeExt parameter' {
        It 'Accepts extension exclusions without error' {
            { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludeExt @('yml', 'json') } | Should -Not -Throw
        }

        It 'Strips leading dots from extensions' {
            { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludeExt @('.yml', '.json') } | Should -Not -Throw
        }

        It 'Accepts empty extension array' {
            { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludeExt @() } | Should -Not -Throw
        }
    }

    Context 'ExcludePath parameter' {
        It 'Accepts path exclusions without error' {
            { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludePath @('docs/', '.github/') } | Should -Not -Throw
        }

        It 'Accepts empty path array' {
            { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludePath @() } | Should -Not -Throw
        }
    }

    Context 'Combined exclusion flags' {
        It 'Accepts markdown, extension, and path exclusions together' {
            { Get-DiffOutput -ComparisonRef 'HEAD~1' -ExcludeMarkdownDiff -ExcludeExt @('yml') -ExcludePath @('docs/') } | Should -Not -Throw
        }
    }
}

Describe 'Get-DiffSummary' {
    It 'Returns shortstat summary string' {
        $result = Get-DiffSummary -ComparisonRef 'HEAD~1'
        $result | Should -BeOfType [string]
    }

    It 'Should throw when diff summary fails' {
        Mock git { $global:LASTEXITCODE = 128; return $null }
        { Get-DiffSummary -ComparisonRef 'main' } | Should -Throw '*Failed to summarize diff output*'
    }

    It 'Should return "0 files changed" when diff summary is empty' {
        Mock git { $global:LASTEXITCODE = 0; return '' }
        $result = Get-DiffSummary -ComparisonRef 'main'
        $result | Should -Be '0 files changed'
    }

    Context 'ExcludeExt parameter' {
        It 'Accepts extension exclusions without error' {
            { Get-DiffSummary -ComparisonRef 'HEAD~1' -ExcludeExt @('yml', 'json') } | Should -Not -Throw
        }
    }

    Context 'ExcludePath parameter' {
        It 'Accepts path exclusions without error' {
            { Get-DiffSummary -ComparisonRef 'HEAD~1' -ExcludePath @('docs/') } | Should -Not -Throw
        }
    }
}

Describe 'Get-PrXmlContent' {
    It 'Returns valid XML string' {
        $result = Get-PrXmlContent -CurrentBranch 'feature/test' -BaseBranch 'main' -CommitEntries @('commit 1', 'commit 2') -DiffOutput @('diff line 1', 'diff line 2')
        $result | Should -Not -BeNullOrEmpty
        $result | Should -Match '<commit_history>'
        $result | Should -Match '</commit_history>'
    }

    It 'Includes branch information' {
        $result = Get-PrXmlContent -CurrentBranch 'feature/my-branch' -BaseBranch 'main' -CommitEntries @() -DiffOutput @()
        $result | Should -Match 'feature/my-branch'
        $result | Should -Match 'main'
    }

    It 'Includes commit entries' {
        $result = Get-PrXmlContent -CurrentBranch 'feature/test' -BaseBranch 'main' -CommitEntries @('abc123 Test commit') -DiffOutput @()
        $result | Should -Match 'abc123 Test commit'
    }

    It 'Handles empty inputs' {
        $result = Get-PrXmlContent -CurrentBranch 'branch' -BaseBranch 'main' -CommitEntries @() -DiffOutput @()
        $result | Should -Not -BeNullOrEmpty
    }
}

Describe 'Get-LineImpact' {
    It 'Parses insertions and deletions from shortstat' {
        $result = Get-LineImpact -DiffSummary '5 files changed, 100 insertions(+), 50 deletions(-)'
        $result | Should -Be 150
    }

    It 'Handles insertions only' {
        $result = Get-LineImpact -DiffSummary '2 files changed, 25 insertions(+)'
        $result | Should -Be 25
    }

    It 'Handles deletions only' {
        $result = Get-LineImpact -DiffSummary '1 file changed, 10 deletions(-)'
        $result | Should -Be 10
    }

    It 'Returns 0 for summary without insertions or deletions' {
        $result = Get-LineImpact -DiffSummary 'no changes'
        $result | Should -Be 0
    }

    It 'Returns 0 for no changes' {
        $result = Get-LineImpact -DiffSummary '0 files changed'
        $result | Should -Be 0
    }
}

Describe 'Get-CurrentBranchOrRef' {
    BeforeAll {
        . (Join-Path -Path $PSScriptRoot -ChildPath '../scripts/generate.ps1')
    }

    It 'Returns branch name when on a branch' {
        # This test runs in a real git repo, so it should return something
        $result = Get-CurrentBranchOrRef
        $result | Should -Not -BeNullOrEmpty
        $result | Should -BeOfType [string]
    }

    It 'Returns string starting with detached@ or branch name' {
        $result = Get-CurrentBranchOrRef
        # Either a branch name or detached@<sha>
        ($result -match '^detached@' -or $result -notmatch '^detached@') | Should -BeTrue
    }

    It 'Should return detached@sha when in detached HEAD state' {
        # Use call sequence to distinguish git commands (cross-platform safe)
        $script:gitCallCount = 0
        Mock git {
            $script:gitCallCount++
            if ($script:gitCallCount -eq 1) {
                # First call: git branch --show-current returns empty (detached)
                $global:LASTEXITCODE = 0
                return ''
            }
            # Second call: git rev-parse --short HEAD returns SHA
            $global:LASTEXITCODE = 0
            return 'abc1234'
        }
        $result = Get-CurrentBranchOrRef
        $result | Should -Be 'detached@abc1234'
    }

    It 'Should return unknown when both branch and rev-parse fail' {
        Mock git {
            $global:LASTEXITCODE = 128
            return $null
        }
        $result = Get-CurrentBranchOrRef
        $result | Should -Be 'unknown'
    }
}

Describe 'Invoke-PrReferenceGeneration' {
    It 'Uses custom OutputPath when specified' {
        $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
        $customPath = Join-Path $tempDir 'custom-output/pr-ref.xml'

        try {
            $result = Invoke-PrReferenceGeneration -BaseBranch 'HEAD~1' -OutputPath $customPath
            $result | Should -BeOfType [System.IO.FileInfo]
            $result.FullName | Should -Be (Resolve-Path $customPath).Path
            Test-Path $customPath | Should -BeTrue
        }
        finally {
            Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
        }
    }

    It 'Returns FileInfo object' {
        # Skip if not in a git repo or no commits to compare
        $commitCount = Get-CommitCount -ComparisonRef 'HEAD~1'
        if ($commitCount -eq 0) {
            Set-ItResult -Skipped -Because 'No commits available for comparison'
            return
        }

        # Determine available base branch - prefer origin/main, fall back to main, then HEAD~1
        $baseBranch = $null
        foreach ($candidate in @('origin/main', 'main', 'HEAD~1')) {
            & git rev-parse --verify $candidate 2>$null | Out-Null
            if ($LASTEXITCODE -eq 0) {
                $baseBranch = $candidate
                break
            }
        }

        if (-not $baseBranch) {
            Set-ItResult -Skipped -Because 'No suitable base branch available for comparison'
            return
        }

        $result = Invoke-PrReferenceGeneration -BaseBranch $baseBranch
        $result | Should -BeOfType [System.IO.FileInfo]
        $result.Extension | Should -Be '.xml'
    }

    It 'Should include markdown exclusion note when ExcludeMarkdownDiff is specified' {
        # Skip if not in a git repo or no commits
        $commitCount = Get-CommitCount -ComparisonRef 'HEAD~1'
        if ($commitCount -eq 0) {
            Set-ItResult -Skipped -Because 'No commits available for comparison'
            return
        }

        $baseBranch = $null
        foreach ($candidate in @('origin/main', 'main', 'HEAD~1')) {
            & git rev-parse --verify $candidate 2>$null | Out-Null
            if ($LASTEXITCODE -eq 0) {
                $baseBranch = $candidate
                break
            }
        }

        if (-not $baseBranch) {
            Set-ItResult -Skipped -Because 'No suitable base branch available for comparison'
            return
        }

        Mock Write-Host {}

        $result = Invoke-PrReferenceGeneration -BaseBranch $baseBranch -ExcludeMarkdownDiff
        $result | Should -BeOfType [System.IO.FileInfo]

        # Verify the markdown exclusion note was output
        Should -Invoke Write-Host -ParameterFilter { $Object -eq 'Note: Markdown files were excluded from diff output' }
    }

    Context 'MergeBase parameter' {
        It 'Generates XML when MergeBase is specified' {
            $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
            $customPath = Join-Path $tempDir 'merge-base-test.xml'
            try {
                Mock Write-Host {}
                $result = Invoke-PrReferenceGeneration -BaseBranch 'HEAD~1' -MergeBase -OutputPath $customPath
                $result | Should -BeOfType [System.IO.FileInfo]
                Should -Invoke Write-Host -ParameterFilter { $Object -eq 'Comparison mode: merge-base' }
            }
            finally {
                Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
            }
        }
    }

    Context 'ExcludeExt parameter' {
        It 'Outputs extension exclusion note' {
            $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
            $customPath = Join-Path $tempDir 'ext-test.xml'
            try {
                Mock Write-Host {}
                $null = Invoke-PrReferenceGeneration -BaseBranch 'HEAD~1' -ExcludeExt @('yml', 'json') -OutputPath $customPath
                Should -Invoke Write-Host -ParameterFilter { $Object -like '*Extensions excluded*' }
            }
            finally {
                Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
            }
        }
    }

    Context 'ExcludePath parameter' {
        It 'Outputs path exclusion note' {
            $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
            $customPath = Join-Path $tempDir 'path-test.xml'
            try {
                Mock Write-Host {}
                $null = Invoke-PrReferenceGeneration -BaseBranch 'HEAD~1' -ExcludePath @('docs/') -OutputPath $customPath
                Should -Invoke Write-Host -ParameterFilter { $Object -like '*Paths excluded*' }
            }
            finally {
                Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
            }
        }
    }

    Context 'BaseBranch auto' {
        It 'Resolves auto to the remote default branch' {
            Mock Write-Host {}
            Mock Resolve-DefaultBranch { return 'origin/main' }
            $tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ([System.Guid]::NewGuid().ToString())
            $customPath = Join-Path $tempDir 'auto-test.xml'
            try {
                $result = Invoke-PrReferenceGeneration -BaseBranch 'auto' -OutputPath $customPath
                $result | Should -BeOfType [System.IO.FileInfo]
                Should -Invoke Resolve-DefaultBranch -Times 1
            }
            finally {
                Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
            }
        }
    }
}

Describe 'Large diff warning' {
    It 'Should output large diff message when line impact exceeds 1000' {
        Mock Test-GitAvailability {}
        Mock Get-RepositoryRoot { return (& git rev-parse --show-toplevel).Trim() }
        Mock Resolve-DefaultBranch { return 'origin/main' }
        Mock Get-CurrentBranchOrRef { return 'feature/test' }
        Mock Resolve-ComparisonReference { return [PSCustomObject]@{ Ref = 'HEAD~1'; Label = 'main' } }
        Mock Get-ShortCommitHash { return 'abc1234' }
        Mock Get-CommitEntry { return @('<commit hash="abc1234" date="2026-01-01"><message><subject><![CDATA[test]]></subject><body><![CDATA[]]></body></message></commit>') }
        Mock Get-CommitCount { return 1 }
        Mock Get-DiffOutput { return @('diff --git a/file.txt b/file.txt') }
        Mock Get-DiffSummary { return '10 files changed, 800 insertions(+), 500 deletions(-)' }
        Mock Set-Content {}
        Mock Get-Content { return @('line1', 'line2') }
        Mock Get-Item { return [System.IO.FileInfo]::new('/tmp/pr-reference.xml') }
        Mock Write-Host {}

        $null = Invoke-PrReferenceGeneration -BaseBranch 'main'

        Should -Invoke Write-Host -ParameterFilter {
            $Object -like '*Large diff detected*'
        }
    }
}

Describe 'Entry-point execution' -Tag 'Integration' {
    It 'Should exit 0 when executed successfully as a script' {
        $scriptPath = Join-Path $PSScriptRoot '../scripts/generate.ps1'
        $null = & pwsh -File $scriptPath -BaseBranch 'HEAD~1' 2>&1
        $LASTEXITCODE | Should -Be 0
    }

    It 'Should exit 1 with error message when generation fails' {
        $scriptPath = Join-Path $PSScriptRoot '../scripts/generate.ps1'
        $null = & pwsh -File $scriptPath -BaseBranch 'nonexistent-branch-xyz-999' 2>&1
        $LASTEXITCODE | Should -Be 1
    }
}
