public/Invoke-AutomatedFacebookPostWithPhoto.ps1

function Invoke-AutomatedFacebookPostWithPhoto {

    [CmdletBinding()]
    param(

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

        [Parameter(Mandatory=$true)]
        [ValidateSet('corners','goals','match','outcomes')]
        [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) {

                if ($Content -eq 'outcomes') {

                    $FixtureArtifactPath = "$Path\fixture-artifact\working-set\$Competition\$Date"
                    $TemplatePath = "$Path\git-hub\sportsmonk-api\templates\outcomes_post.txt"
                    
                }
                else {

                    $FixtureArtifactPath = "$Path\fixture-artifact\working-set\$Competition\$Date\$Content"

                } # if

                # Import the results
                $FixtureResults = Import-Csv -Path $ResultsPath -ErrorAction Stop

                # Check the artifact path exists
                $FixtureArtifactPathExists = Test-Path -Path $FixtureArtifactPath

                $Hash1 =@{
                    Competition = $Competition
                    FunctionName = $($MyInvocation.MyCommand.Name)
                    Content = $Content
                    FixtureArtifactPathExists = $FixtureArtifactPathExists
                    FixtureResultCount = $($FixtureResults.Count)
                    Post = 'facebook-image'
                }

                $Hash1 | Format-Table

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

                if ($Content -eq 'outcomes') {

                    # Get the fixture artifact in the root
                    $FixtureArtifacts = Get-ChildItem -Path $FixtureArtifactPath -File -Filter '*.png' -ErrorAction Stop

                }
                else {

                    # Get the fixture artifacts in subfolders
                    $FixtureArtifacts = Get-ChildItem -Path $FixtureArtifactPath -Recurse -File -Filter '*.png' -ErrorAction Stop

                } # if
    
                # 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

                    if ($Content -eq 'outcomes') {

                        $FileCount = 1
                        $TemplatePath = "$Path\git-hub\sportsmonk-api\templates\outcomes_post.txt"
                        $TmplatePathExists = Test-Path -Path $TemplatePath

                        if ($TmplatePathExists) {

                            foreach ($FixtureArtifact in $FixtureArtifacts) {

                                $PSObject = [PSCustomObject]@{
                                    Competition = $Competition
                                    Date = $Date
                                    PhotoPath = $($FixtureArtifact.FullName)
                                    FileCount = $FileCount
                                    Content = $Content
                                    Post = 'facebook-image'
                                }
            
                                $PSObject | Format-List
                                $Template = Get-Content -Path $TemplatePath -Raw
                                $Template = $Template.Replace('<CompetitionName>',$CompetitionName)
                                $Template = $Template.Replace('<FormattedDate>',$FormattedDate)
                                $Template

                                $FilesProcessed = $false
                                $OutcomeFilesToProcess = Confirm-ProcessedFootballFixtureMedia -Competition $Competition -Date $Date -Content $Content -Endpoint fbpost -Path $Path
    
                                if ($($FixtureArtifacts.Count) -eq $($OutcomeFilesToProcess.Count)) {

                                    $FilesProcessed = $true

                                } # if

                                $Hash2 =@{
                                    CompetitionName = $CompetitionName
                                    Date = $Date
                                    Content = $Content
                                    FixtureArtifacts = $($FixtureArtifacts.Count)
                                    FilesToProcesss = $($OutcomeFilesToProcess.Count)
                                    FilesProcessed = $FilesProcessed
                                }

                                $Hash2 | Format-Table

                                if ($Post -and !$FilesProcessed) {

                                    if ($Post) {

                                        [int]$DateYear = $Date.Split('-')[0]
                                        [int]$DateMonth = $Date.Split('-')[1]
                                        $MLogPath = Get-ProcessedFootballFixtureFolderPath -Competition $Competition -DateYear $DateYear -DateMonth $DateMonth -Type media -Path $Path

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

                                    } # if

                                } # if

                            } # foreach

                        } # if

                    } # if
                
                    foreach ($FixtureResult in $FixtureResults) {
                        
                        $FixtureNameString = $($FixtureResult.FixtureName).Replace(' ','-')
                        $FilePrefix = "$($FixtureResult.fixture_id)-$FixtureNameString"

                        if ($Content -eq 'match') {

                            $FixtureArtifact = $FixtureArtifacts | Where-Object {$_.Name -like "$FilePrefix-twitter-review*"}

                        }
                        else {

                            $FixtureArtifact = $FixtureArtifacts | Where-Object {$_.Name -like "$FilePrefix-twitter-$Content*"}

                        } # if
            
                        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
                                    Post = '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

                                $FixtureProcessed = $false
                                $FixturesToProcess = Confirm-ProcessedFootballFixtureMedia -Competition $Competition -Date $Date -Content $Content -Endpoint fbpost -Path $Path

                                if ($($FixturesToProcess.fixture_Id) -notcontains $($FixtureResult.fixture_id)) {

                                    $FixtureProcessed = $true

                                } # if

                                $Hash3 =@{
                                    FixtureId = $($FixtureResult.fixture_id)
                                    FixtureName = $($FixtureResult.FixtureName)
                                    Content = $Content
                                    FixtureProcessed = $FixtureProcessed
                                }

                                $Hash3 | Format-Table

                                if ($Post -and !$FixtureProcessed) {

                                    if ($Post) {

                                        [int]$DateYear = $Date.Split('-')[0]
                                        [int]$DateMonth = $Date.Split('-')[1]
                                        $MLogPath = Get-ProcessedFootballFixtureFolderPath -Competition $Competition -DateYear $DateYear -DateMonth $DateMonth -Type media -Path $Path

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

                                    } # if

                                } # if

                            } # if

                        } # if

                    } # foreach

                } # if

            } # if

        }
        catch {

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

        } # try catch

    } # process

} # function