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,

        [Parameter(Mandatory=$true)]
        [ValidateSet('image','story')]
        [string]$Type

    )
    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 =@{
                    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) {

                                $Hash2 =@{
                                    Competition = $Competition
                                    Date = $Date
                                    FixtureName = $($FixtureResult.FixtureName)
                                    PhotoPath = $($FixtureArtifact.FullName)
                                    Type = $Type
                                    Content = $Content
                                }
    
                                $Hash2 | Format-Table
    
                                $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

                                if ($Post) {

                                    Invoke-FacebookPostWithPhoto -PageId $PageId -Token $Token -Message $Template -Type $Type -Path $($FixtureArtifact.FullName)

                                } # if

                            } # if

                        } # if

                    } # foreach

                } # if

            } # if

        }
        catch {

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

        } # try catch

    } # process

} # function