public/Invoke-AutomatedFacebookPostWithPhoto.ps1

function Invoke-AutomatedFacebookPostWithPhoto {

    [CmdletBinding()]
    param(

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

        [Parameter(Mandatory=$true)]
        [ValidateSet('corners','goals','match')]
        [string]$Content,

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

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

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

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

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

    )
    process{

        $ErrorActionPreference = 'Stop'


        try {

            # Import the fixtures from the results file
            $ResultsPath = "$Path\sportsmonk-results\$Competition\$Date\$Competition-$Date.csv"
            $ResultsPathExists = Test-Path -Path $ResultsPath

            if ($ResultsPathExists) {

                $FixtureResults = Import-Csv -Path $ResultsPath -ErrorAction Stop

                # Check the artifact path exists
                $FixtureArtifactPath = "$Path\fixture-artifact\working-set\$Competition\$Date\$Content"
                $FixtureArtifactPathExists = Test-Path -Path $FixtureArtifactPath

                $Hash1 =@{
                    Competition = $Competition
                    FunctionName = $($MyInvocation.MyCommand.Name)
                    Content = $Content
                    FixtureArtifactPathExists = $FixtureArtifactPathExists
                    FixtureResultCount = $($FixtureResults.Count)
                }

                $Hash1 | Format-Table

            } # if
                
            if ($FixtureArtifactPathExists -and $($FixtureResults.Count) -ge 1) {

                # Get the fixture artifacts
                $FixtureArtifacts = Get-ChildItem -Path $FixtureArtifactPath -Recurse -File -ErrorAction Stop

                # Only if there are artifacts proceed
                if ($($FixtureArtifacts.Count) -ge 1) {

                    # Convert string to DateTime object
                    $DateObject = [DateTime]::ParseExact($Date, 'yyyy-MM-dd', $null)

                    # Format the DateTime object to the desired format 2025-04-06 becomes 06-04-2025
                    $FormattedDate = $DateObject.ToString('dd-MM-yyyy')

                    # Get Competition name
                    $CompetitionName = Get-Competition -Name $Competition

                    foreach ($FixtureResult in $FixtureResults) {

                        $FixtureNameString = $($FixtureResult.FixtureName).Replace(' ','-')
                        $FilePrefix = "$($FixtureResult.fixture_id)-$FixtureNameString"
                        $FixtureArtifact = $FixtureArtifacts | Where-Object {$_.Name -like "$FilePrefix*"}

                        if ($($FixtureArtifact.Count) -eq 1) {
                            
                            $TemplatePath = "$Path\git-hub\sportsmonk-api\templates\social_media.txt"
                            $TmplatePathExists = Test-Path -Path $TemplatePath

                            if ($TmplatePathExists) {

                                $PSObject = [PSCustomObject]@{
                                    Competition = $Competition
                                    Date = $Date
                                    FixtureId = $($FixtureResult.fixture_id)
                                    FixtureName = $($FixtureResult.FixtureName)
                                    PhotoPath = $($FixtureArtifact.FullName)
                                    Content = $Content
                                    Endpoint = 'facebook-image'
                                }
            
                                $PSObject | Format-List

                                $Template = Get-Content -Path $TemplatePath -Raw
                                $Template = $Template.Replace('<Content>',$Content)
                                $Template = $Template.Replace('<CompetitionName>',$CompetitionName)
                                $Template = $Template.Replace('<FormattedDate>',$FormattedDate)
                                $Template = $Template.Replace('<FixtureName>',$($FixtureResult.FixtureName))
                                $Template

                                [int]$DateYear = $Date.Split('-')[0]
                                [int]$DateMonth = $Date.Split('-')[1]
                                $MFileDateTime = Get-Date -Format FileDateTime
                                $MLogPath = Get-ProcessedFootballFixtureFolderPath -Competition $Competition -DateYear $DateYear -DateMonth $DateMonth -Type media -Path $Path
                                $FilesToProcess = Get-ChildItem -Path $MLogPath | Where-Object {$_.FullName -like "*$Content-fbpost*"}
                                $FixtureProcessed = $false

                                foreach ($File in $($FilesToProcess.FullName)) {

                                    Write-CustomWarningMessage -ParentPath $Path -FilePath $File
                                    $FileContent = Get-Content $File | ConvertFrom-Json

                                    if ($($FileContent.FixtureId) -eq $($FixtureResult.fixture_id)) {

                                        $FixtureProcessed = $true
                                        
                                    } # if

                                } # foreach

                                if ($Post -and !$FixtureProcessed) {

                                    if ($Post) {

                                        Invoke-FacebookPostWithPhoto -PageId $PageId -Token $Token -Message $Template -Path $($FixtureArtifact.FullName)
                                        $PSObject| ConvertTo-Json | Set-Content -Path "$MlogPath\$MFileDateTime-$Competition-football-fixture-$Content-fbpost.json"

                                    } # if

                                } # if

                            } # if

                        } # if

                    } # foreach

                } # if

            } # if

        }
        catch {

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

        } # try catch

    } # process

} # function