public/Invoke-PopulateFootballCompetitionTemplateAutomation.ps1

function Invoke-PopulateFootballCompetitionTemplateAutomation {
    <#
        .EXAMPLE
            Invoke-PopulateFootballCompetitionTemplateAutomation -Header $Headers -Token $Token -Continent north-america -Date 2025-02-25 -Type review -Path C:\sportsmonk
     
    #>

    [CmdletBinding()]
    param(

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

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

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

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

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

        [Parameter(Mandatory=$true)]
        [ValidateSet('preview','review')]
        [string]$Type

    )
    process{

        $ErrorActionPreference = 'Stop'
     
        $Competions = Select-FootballCompetition -Continent $Continent
        $Competions.GetEnumerator() | ForEach-Object -Process {

            $Competition = $($_.key)
            $CompetitionDate = (Get-ChildItem -Path "$Path\sportsmonk-results\$Competition" -ErrorAction Stop).Name
            $StartProcessing = $false

            foreach ($Date in $CompetitionDate) {

                $DateToUse = Get-Date -Date $Date
                $StartDateToUse = Get-Date -Date $StartDate

                if ($DateToUse -ge $StartDateToUse) {

                    Write-Warning "Processing: $Competition - $Date"

                    # All files
                    $ProcessedFiles = Confirm-FootballFixtureArtifact -Type review-template -Path $Path `
                        | Where-Object {$_.Competition -eq $Competition -and $_.Date -eq $Date}

                    # False
                    $ProcessedFilesFalse = $ProcessedFiles | Where-Object {$_.IsCompleted -eq $false}

                    # True
                    $ProcessedFilesTrue = $ProcessedFiles | Where-Object {$_.IsCompleted -eq $true}

                    if ($($ProcessedFilesTrue.Count) -lt 1 -and $($ProcessedFilesFalse.Count) -lt 1) {

                        # Display
                        $ProcessedFiles | Format-Table

                        # Set start processing
                        $StartProcessing = $true

                    } # if

                    if ($($ProcessedFilesFalse.count) -ge 1) {

                        # Display
                        $ProcessedFiles | Format-Table

                        # Set start processing
                        $StartProcessing = $true

                    } # if
          
                } # if

                if ($StartProcessing) {

                    $ResultsPathExists = $false
                    $PredictionsPathExists = $false
                    $ResultsPathExists = Test-Path -Path "$Path\sportsmonk-results\$($_.Key)\$Date"
                    $PredictionsPathExists = Test-Path -Path "$Path\sportsmonk-predictions\$($_.Key)\$Date"

                    $Hash1 =@{
                        FunctionName = $($MyInvocation.MyCommand.Name)
                        Competition = $($_.key)
                        Date = $Date
                        ResultsPathExists = $ResultsPathExists
                        PredictionsPathExists = $PredictionsPathExists
                    }

                    $Hash1 | Format-Table

                    if ($PredictionsPathExists) {

                        New-FootballFixtureFolder -Competition $($_.Key) -Date $Date -Path $Path

                        # Must run before Get-FootballFixturePredictionPreviewData.
                        Get-FootballFixturePredictedScoreCardData -Competition $($_.Key) -Date $Date -Path $Path
                        Get-FootballFixturePredictionPreviewData -Header $Header -Token $Token -Competition $($_.Key) -Date $Date -Path $Path

                    } # if

                    if ($Type -eq 'preview' -and $PredictionsPathExists) {

                        Get-FootballFixtureMarketTypeProbabilityPercentage -Header $Header -Token $Token -Date $Date -Competition $($_.Key) -Preview -Path $Path
                        Update-FootballFixtureTemplate -Header $Header -Token $Token -Date $Date -Competition $($_.Key) -Template prediction-preview -Path $Path
                        Update-FootballFixtureTemplate -Header $Header -Token $Token -Date $Date -Competition $($_.Key) -Template prediction-corner-preview -Path $Path

                    } # if

                    if ($Type -eq 'review' -and $ResultsPathExists -and $PredictionsPathExists) {

                        Get-FootballFixtureMarketTypeProbabilityPercentage -Header $Header -Token $Token -Date $Date -Competition $($_.Key) -Path $Path
                        Update-FootballFixtureTemplate -Header $Header -Token $Token -Date $Date -Competition $($_.Key) -Template prediction-review -Path $Path
                        Update-FootballFixtureTemplate -Header $Header -Token $Token -Date $Date -Competition $($_.Key) -Template prediction-corner-review -Path $Path
                        Update-FootballFixtureTemplate -Header $Header -Token $Token -Date $Date -Competition $($_.Key) -Template prediction-goal-review -Path $Path
                        Update-FootballFixtureTemplate -Header $Header -Token $Token -Date $Date -Competition $($_.Key) -Template outcome -Path $Path
                        
                        # Check if path exists
                        $PathExists = Test-Path -Path "$Path\sportsmonk-predictions\$Competition\$Date"

                        if ($PathExists) {
                            
                            $FilePath = "$Path\temp\results\$Competition\$Date"
                            New-Directory -Path $FilePath

                            # First goal scorer
                            Get-FootballFixtureGoalScorer -Header $Header -Token $Token -Competition $Competition -Date $Date -Path $Path | Out-File "$FilePath\$Competition-$Date-first-goal.txt"

                            # HTFT
                            Get-FootballFixtureHTFTProbability -Competition $Competition -Date $Date -Path $Path | Out-File "$FilePath\$Competition-$Date-htft.txt"

                            # Correct score
                            Get-FootballFixtureCorrectScoreProbability -Competition $Competition -Date $Date -Path $Path | Out-File "$FilePath\$Competition-$Date-correctscore.txt"

                            # Social media corners
                            New-SocialMediaContent -Competition $Competition -Date $Date -Content corners -Path $Path | Out-File "$FilePath\$Competition-$Date-corners.txt"

                            # Social media goals
                            New-SocialMediaContent -Competition $Competition -Date $Date -Content goals -Path $Path | Out-File "$FilePath\$Competition-$Date-goals.txt"

                            # Social media match
                            New-SocialMediaContent -Competition $Competition -Date $Date -Content match -Path $Path | Out-File "$FilePath\$Competition-$Date-match.txt"

                            # social media outcomes
                            New-SocialMediaContent -Competition $Competition -Date $Date -Content outcomes -Path $Path | Out-File "$FilePath\$Competition-$Date-outcomes.txt"

                            # Create perdiction review content
                            New-SocialMediaContent -Competition $Competition -Date $Date -PredictionReview -Path $Path | Out-File "$FilePath\$Competition-$Date-prediction-review.txt"

                        } # if

                    } # if
            
                } # if

            } # foreach

        } # foreach-object

    } # process

} # function