public/Get-FootballFixturePredictionPreviewData.ps1

function Get-FootballFixturePredictionPreviewData {
    <#
        .EXAMPLE
            Get-FootballFixturePredictionPreviewData -Header $Headers -Token $Token -Competition $Competition -Date $Date -Path C:\sportsmonk
 
        .EXAMPLE
            Get-FootballFixturePredictionPreviewData -Header $HeaderS -Token $Token -Competition $Competition -Date $Date -Refresh -Path C:\sportsmonk
             
    #>

    [CmdletBinding()]
    param(

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Competition,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Date,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [Object]$Header,
        
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Path,

        [Parameter(Mandatory=$false)]
        [switch]$Refresh,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Token

    )
    process{

        $ErrorActionPreference = 'Stop'

        try {

            # Predictions folder.
            $PredicitionsFolder = "$Path\sportsmonk-predictions\$Competition\$Date"
            $PredicitionsFolderExists = Test-Path -Path $PredicitionsFolder

            # Results folder.
            $ResultsFolder = "$Path\sportsmonk-results\$Competition\$Date"
            $ResultsFolderExists = Test-Path -Path $ResultsFolder

            if ($PredicitionsFolderExists) {

                if ($Refresh) {

                    # Data lag issues.
                    Remove-Item -Path "$Path\fixture-artifact\prediction-preview\$Competition\$Date" -Recurse -Force -Confirm:$false

                } # if

                # Get the list of files in the predictions folder.
                $FilesToCheck = Get-ChildItem -Path $PredicitionsFolder

                # Select the first file and get the fixtures.
                $Fixtures = Import-Csv -Path ($FilesToCheck.FullName)[0]
                $TopScores = "$Path\game-predictions-outcomes\$Competition\$Date\$Competition-240-$Date.csv"

                if ($ResultsFolderExists) {

                    # C:\sportsmonk\sportsmonk-results\esp-laliga\2025-01-11\esp-laliga-2025-01-11.csv
                    $ResultFile = Get-ChildItem -Path $ResultsFolder

                    if ($($ResultFile.Count) -lt 1) {

                        throw "Result file count: $($ResultFile.Count)."

                    } # if

                    # C:\sportsmonk\game-predictions-outcomes\esp-laliga\2025-01-11\esp-laliga-240-2025-01-11.csv
                    $TopScores = "$Path\game-predictions-outcomes\$Competition\$Date\$Competition-240-$Date.csv"

                }
                else {

                    Write-Warning -Message "Folder does not exist: $ResultsFolder."

                } # if

            }
            else {

                Write-Warning -Message "Folder does not exist: $PredicitionsFolder."

            } # if

            # Loop through each fixture in the first file.
            foreach ($Fixture in $Fixtures) {
                
                # Loop through the market type files.
                foreach ($MarketType in $FilesToCheck) {

                    $MarketTypeFileExists = Test-Path -Path $($MarketType.FullName)

                    if (!$MarketTypeFileExists) {

                        Write-Warning -Message "File not exist: $($MarketType.FullName)."
                        continue

                    } # if

                    # Get the specfic fixture from the market type file.
                    $MarketTypeFixture = Import-Csv -Path $($MarketType.FullName) | Where-Object {$_.FixtureId -eq $($Fixture.FixtureId)}

                    # esp-laliga-239-2025-01-11.csv
                    $FileName = $($MarketType.FullName).Split('\')[-1]

                    if ($($Fixture.FixtureName) -like '*Glimt*') {

                        Write-Warning -Message "Skipping $($Fixture.FixtureName)."
                        continue

                    } # if

                    # Remove spaces in the fixture name.
                    $FixtureName = $($Fixture.FixtureName).Replace(' ','-')

                    # C:\sportsmonk\fixture-artifact\prediction-preview\eng-premier\2025-02-15\19134573-Aston-Villa-vs-Ipswich-Town
                    $PathToUse = "$Path\fixture-artifact\prediction-preview\$Competition\$Date\$($Fixture.FixtureId)-$FixtureName"
                    $PathToUseExists = Test-Path -Path $PathToUse

                    if (!$PathToUseExists) {

                        New-Item -Path $PathToUse -ItemType Directory -Verbose | Out-Null

                    } # if

                    if ($($MarketType.FullName) -like '*232*') {

                        $HTFTProbability = Get-FootballFixtureHTFTProbability -Competition $Competition -Date $Date -Path $Path | Where-Object {$_.FixtureId -eq $($Fixture.FixtureId)}
                        $HTFTProbability | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-htft-probability-$date.csv" -Force -Verbose
                        
                    } # if
                    
                    $MarketTypeFixture | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-$FileName" -Force -Verbose

                    # Move this.
                    #$FixtureTopScores = Import-Csv -Path $TopScores | Where-Object {$_.FixtureId -eq $($Fixture.FixtureId)}
                    #$FixtureTopScores | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-$Competition-top-scores-$Date.csv" -Force -Verbose

                    #$FixtureGoals = Get-FootballFixtureEvent -Header $Headers -Token $Token -FixtureId $($Fixture.FixtureId) -Event goal
                    #$FixtureGoals | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-goals-$Date.csv" -Force
    
                } # foreach

                # Remove spaces in the fixture name.
                $FixtureName = $($Fixture.FixtureName).Replace(' ','-')

                # C:\sportsmonk\fixture-artifact\prediction-preview\eng-premier\2025-02-15\19134573-Aston-Villa-vs-Ipswich-Town
                $PathToUse = "$Path\fixture-artifact\prediction-preview\$Competition\$Date\$($Fixture.FixtureId)-$FixtureName"
                $PathToUseExists = Test-Path -Path $PathToUse

                if ($PathToUseExists) {

                    # Top scores.
                    $FixtureTopScores = Import-Csv -Path $TopScores | Where-Object {$_.FixtureId -eq $($Fixture.FixtureId)}
                    $FixtureTopScores | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-$Competition-top-scores-$Date.csv" -Force -Verbose

                    # Goals.
                    $FixtureGoals = Get-FootballFixtureEvent -Header $Header -Token $Token -FixtureId $($Fixture.FixtureId) `
                        | Where-Object {$_.sort_order -eq 1 -and ($_.type_id -eq 14 -or $_.type_id -eq 15 -or $_.type_id -eq 16)}
                        
                    $FixtureGoals | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-goals-$Date.csv" -Force

                    # Corners.
                    $FixtureCorners = Get-FootballFixtureCornerCount -Header $Header -Token $Token -Competition $Competition -Date $Date -Path $Path `
                        | Where-Object {$_.FixtureId -eq $($Fixture.FixtureId)}
                        
                    $FixtureCorners | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-corners-$Date.csv" -Force

                    foreach ($Result in $ResultFile) {

                        $ResultFileExists = Test-Path -Path $($Result.FullName)

                        if (!$ResultFileExists) {

                            Write-Warning -Message "File not exist: $($Result.FullName)."
                            continue

                        } # if

                        Write-Warning -Message "$($MyInvocation.MyCommand.Name): Processing $($Fixture.FixtureId)"
                        $FixtureResult = Import-Csv -Path $($Result.FullName) | Where-Object {$_.Fixture_Id -eq $($Fixture.FixtureId)}

                        if ($($FixtureResult.Count) -ge 1) {

                            # esp-laliga-2025-01-11.csv
                            $FileName = $($Result.FullName).Split('\')[-1]
                            $FileNameToUse = $FileName.Replace("$Date.csv","results-$Date.csv")
                            # Remove spaces in the fixture name.
                            $FixtureName = $($Fixture.FixtureName).Replace(' ','-')
                            $FixtureResult | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-$FileNameToUse" -Force -Verbose

                            $FTResult = "$($FixtureResult.HomeScore)-$($FixtureResult.AwayScore)"
                            $ResultProbability = Get-FootballFixtureResultProbability -Competition $Competition -Date $Date -FixtureId $($Fixture.FixtureId) -Result $FTResult -Path $Path
                            
                            $FileNameToUse = $FileName.Replace("$Date.csv","results-probability-$Date.csv")
                            $ResultProbability | Export-Csv -Path "$PathToUse\$($Fixture.FixtureId)-$FixtureName-$FileNameToUse" -Force -Verbose

        
                        }
                        else {

                            Write-Warning -Message "Fixture results count: $($FixtureResult.Count)."

                        } # if

                    } # foreach

                } # if

            } # foreach

        }
        catch {

            "$($MyInvocation.MyCommand.Name): $_.Exception.Message"

        } # trycatch

    } # process

} # function