<?php

namespace Tricks\CampaignMonitor;

use CS_REST_Transactional_SmartEmail;
use CS_REST_Wrapper_Result;

class CampaignMonitor
{
    /** @var CS_REST_Transactional_SmartEmail */
    protected $campaignMonitorTransactional;

    public function __construct(CS_REST_Transactional_SmartEmail $campaignMonitorTransactional)
    {
        $this->campaignMonitorTransactional = $campaignMonitorTransactional;
    }

    /**
     * @param string $smartEmailId
     * @param string $smartEmailRecipient
     * @param array $messageData
     * @param array $attachments
     * @return CS_REST_Wrapper_Result
     */
    public function sendTransactional(
        string $smartEmailId,
        string $smartEmailRecipient,
        array $messageData,
        array $attachments = []
    ): CS_REST_Wrapper_Result {

        $this->campaignMonitorTransactional->set_smartemail_id($smartEmailId);

        $to = explode(';', $smartEmailRecipient);
        $to = array_map('trim', $to);

        $simpleMessage = [
            "To" => $to,
            "Data" => $messageData,
        ];

        if (!empty($attachments)) {
            $simpleMessage['Attachments'] = $attachments;
        }

        $consentToTrack = 'no'; # Valid: 'yes', 'no', 'unchanged'

        return $this->campaignMonitorTransactional->send($simpleMessage, $consentToTrack);
    }
}
