public/Get-FootballFixturePredictionPreviewData.ps1

function Get-FootballFixturePredictionPreviewData {
    <#
        .SYNOPSIS
            Create CSV files for twitter preview.
 
    #>

    [CmdletBinding()]
    param(

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

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

    )
    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) {

                # 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

                    $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

                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

                    }
                    else {

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

                    }

                } # foreach

            } # foreach

        }
        catch {

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

        } # trycatch

    } # process

} # function