public/Invoke-FootballCompetitionStatisticAutomation.ps1

function Invoke-FootballCompetitionStatisticAutomation {
    <#
        .EXAMPLE
            Invoke-FootballCompetitionStatisticAutomation -Header $Headers -Token $Token -Continent europe-uk -YearStart 2024 -YearEnd 2025 -Type season -Path C:\sportsmonk
 
        .EXAMPLE
            Invoke-FootballCompetitionStatisticAutomation -Header $Headers -Token $Token -Continent europe-uk -YearStart 2024 -YearEnd 2025 -Type season -Refresh -Path C:\sportsmonk
 
    #>

    [CmdletBinding()]
    param(  

        [Parameter(Mandatory=$true)]
        [ValidateSet('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=$false)]
        [switch]$Refresh,

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

        [Parameter(Mandatory=$true)]
        [ValidateSet('stage','season')]
        [string]$Type,

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

        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [int]$YearStart

    )
    process{

        $ErrorActionPreference = 'Stop'

        try {

            if ($Refresh) {
                
                Remove-Item -Path "$Path\fixture-artifact\stage-statistic\$Continent" -Recurse -Force -Confirm:$false -ErrorAction Stop

            } # if

            $CompetitionStage = Get-FootballCompetitionStage -Header $Header -Token $Token -YearStart $YearStart -YearEnd $YearEnd -Continent $Continent

            foreach ($Stage in $CompetitionStage) {

                # Skip continue in the object
                if ($($Stage.sport_id) -eq 1) {

                    $Stage | Format-List

                    if ($Type -eq 'stage') {
                        
                        $CompetitionStageStats = Get-FootballCompetitionStageStatistic -Header $Header -Token $Token -StageId $($Stage.id)

                    } # if

                    if ($Type -eq 'season') {
                        
                        $CompetitionStageStats = Get-FootballCompetitionStageStatistic -Header $Header -Token $Token -SeasonId $($Stage.season_id)

                    } # if

                    $CompetitionStageStatsToUse = $CompetitionStageStats | Where-Object {$_.model_id -eq $($Stage.id)}

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

                        $StatisticObject = New-FootballCompetitionStageStatisticObject -StageObject $Stage -StatisticObject $CompetitionStageStatsToUse

                        Foreach ($Object in $StatisticObject) {

                            if ($($Object.s_sport_id) -eq 1) {

                                $PathToUse = "$Path\fixture-artifact\stage-statistic\$Continent\$YearStart\$($Object.type_id)-$($Object.t_code)"
                                New-Directory -Path $PathToUse
                                $Object | Export-Csv -Path "$PathToUse\$($Object.s_league_id)-$($Object.type_id)-$($Object.t_code).csv" -Force

                            } # if
                        
                        } # foreach

                    } # if

                } # if

            } # foreach

        }
        catch {

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

        } # try catch

    } # process

} # function