public/Invoke-FootballCompetitionFixtureAutomation.ps1

function Invoke-FootballCompetitionFixtureAutomation {
    <#
        .SYNOPSIS
            Retrieves predictons and results.
 
        .EXAMPLE
            Invoke-FootballCompetitionFixtureAutomation -Header $Headers `
                                                        -Token $Token `
                                                        -DateRange '2025-02-24/2025-02-28' `
                                                        -DateMonth 2 `
                                                        -DateYear 2025 `
                                                        -DaysToRun 16 `
                                                        -Type predictions `
                                                        -Path C:\sportsmonk
 
        .EXAMPLE
            Invoke-FootballCompetitionFixtureAutomation -Header $Headers `
                                                        -Token $Token `
                                                        -Competition eng-premier `
                                                        -DateRange '2025-02-24/2025-02-28' `
                                                        -DateMonth 2 `
                                                        -DateYear 2025 `
                                                        -DaysToRun 16 `
                                                        -Type results `
                                                        -Path C:\sportsmonk
 
        .INPUTS
            - [Object] FixtureList
            - [Object] Header
         
    #>

    [CmdletBinding()]
    param(  

        [Parameter(Mandatory=$true)]
        [ValidateSet('europe','europe-uk','south-america','north-america','asia')]
        [string]$Continent,

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

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [int]$DateMonth,

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

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [int]$DateYear,

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [int]$DaysToRun,

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

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

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

        [Parameter(Mandatory=$true)]
        [ValidateSet('predictions','results')]
        [string]$Type

    )
    process{

        $ErrorActionPreference = 'Stop'

        # Get range of the fixtures
        $FixtureList =@()
        $FixtureList = Get-FixtureList -Header $Headers -Token $Token -DateRange $DateRange -Continent $Continent
        $FixtureList | Select-Object -Property id,name,starting_at,result_info | Format-Table

        # Exclude processed fixtures
        $NewFixturesToProcess =@()
        $NewFixturesToProcess = Confirm-ProcessedFootballFixture -FixtureList $FixtureList `
                                                                 -Continent $Continent `
                                                                 -DateMonth $DateMonth `
                                                                 -DateYear $DateYear `
                                                                 -Type $Type `
                                                                 -Path $Path

        $NewFixturesToProcess | Select-Object -Property id,name,starting_at,result_info | Sort-Object -Property id -Descending | Format-Table
        $Hash1 =@{AllFixturesCount = $($AllFixtures.Count);FixtureListCount = $($FixtureList.Count)}
        $Hash1

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

            # Get all the predictions for the fixtures
            $FixtureListPrediction =@()
            $FixtureListPrediction += Get-FootballPredictionProbability -Header $Headers -Token $Token -FixtureList $NewFixturesToProcess

            # Get the competitions
            $Competitions = $Competitions = Select-FootballCompetition -Continent $Continent
            $Hash2 =@{CompetionCount = $($Competitions.Count)} 
            $Hash2

        }
        else {

            Write-Warning -Message "NewFixturesToProcessCount: $($NewFixturesToProcess.Count)"
            break

        } # if

        $Competitions.GetEnumerator() | ForEach-Object -Process {

            Write-Warning -Message "Processing $_."
            New-ProcessedFootballFixtureFolder -Competition $($_.Key) -DateYear $DateYear -DateMonth $DateMonth -Path $Path
            $CompetitionMatched = $false

            if ($($PSBoundParameters.ContainsKey('Competition'))) {

                if ($($_.Key) -eq $Competition) {
                
                    $CompetitionsToUse = $($_.Key)
                    $CompetitionMatched = $true

                }
      
            }
            else {

                $CompetitionsToUse = $($_.Key)
                $CompetitionMatched = $true
                
            } # if

            if ($Type -eq 'predictions' -and $CompetitionMatched) {

                Get-FootballCompetitionPrediction -Header $Headers `
                                                  -Token $Token `
                                                  -Competition $CompetitionsToUse `
                                                  -DateMonth $DateMonth `
                                                  -DateYear $DateYear `
                                                  -DaysToRun $DaysToRun `
                                                  -FixtureList $FixtureList `
                                                  -FixtureListPrediction $FixtureListPrediction `
                                                  -Path $Path

                $identifier = Get-LeagueIdentifier -Competition $CompetitionsToUse
                $CompetitionFixtureList = $FixtureList | Where-Object {$_.league_id -eq $identifier}
                $PFileDateTime = Get-Date -Format FileDateTime
                # "$Path\fixture-artifact\processed-fixtures\$DateYear\$DateMonthToUse\$Type\$Competition"
                $PlogPath = Get-ProcessedFootballFixtureFolderPath -Competition $CompetitionsToUse -DateYear $DateYear -DateMonth $DateMonth -Type $Type -Path $Path
                $CompetitionFixtureList | ConvertTo-Json | Set-Content -Path "$PlogPath\$PFileDateTime-$CompetitionsToUse-football-fixture-$Type.json"
                
            } # if

            if ($Type -eq 'results' -and $CompetitionMatched) {

                Get-FootballCompetitionResult -Header $Headers `
                                              -Token $Token `
                                              -Competition $CompetitionsToUse `
                                              -DateMonth $DateMonth `
                                              -DateYear $DateYear `
                                              -DaysToRun $DaysToRun `
                                              -FixtureList $FixtureList `
                                              -FixtureListPrediction $FixtureListPrediction  `
                                              -Path $Path
            
                $identifier = Get-LeagueIdentifier -Competition $CompetitionsToUse
                $CompetitionFixtureList = $FixtureList | Where-Object {$_.league_id -eq $identifier}
                $RFileDateTime = Get-Date -Format FileDateTime
                # "$Path\fixture-artifact\processed-fixtures\$DateYear\$DateMonthToUse\$Type\$Competition"
                $RlogPath = Get-ProcessedFootballFixtureFolderPath -Competition $CompetitionsToUse -DateYear $DateYear -DateMonth $DateMonth -Type $Type -Path $Path
                $CompetitionFixtureList | ConvertTo-Json | Set-Content -Path "$RlogPath\$RFileDateTime-$CompetitionsToUse-football-fixture-$Type.json"
            
            } # if
    
        } # foreach-object

        $FixtureList  | Select-Object -Property league_id,name,starting_at

    } # process

} # function