<?php
/**
 * The legally required acknowledgement of receipt.
 *
 * @package SkyServers\Retragere
 */

declare(strict_types=1);

namespace SkyServers\Retragere\Wc\Emails;

use SkyServers\Retragere\Core\EmailComposer;
use SkyServers\Retragere\Wp\Repository;

/**
 * This is the one email the law requires. It must reproduce the declaration in
 * full, with the date and time it was submitted.
 */
final class CustomerEmail extends BaseEmail
{
    public function __construct()
    {
        $this->id             = 'ssrc_customer_confirmation';
        $this->customer_email = true;
        $this->title          = __('Contract withdrawal confirmation', 'skyservers-retragere-contract');
        $this->description    = __('The acknowledgement sent to the consumer. Required by law.', 'skyservers-retragere-contract');
        $this->heading        = __('We have received your request', 'skyservers-retragere-contract');
        $this->subject        = __('Withdrawal request received', 'skyservers-retragere-contract');

        parent::__construct();
    }

    public function dispatch(int $recordId): bool
    {
        $this->recordId = $recordId;
        $composed       = $this->compose($recordId);

        if (null === $composed) {
            return false;
        }

        $this->bodyHtml  = $composed['html'];
        $this->bodyText  = $composed['text'];
        $this->recipient = (string) get_post_meta($recordId, Repository::META_EMAIL, true);

        if (!$this->is_enabled() || '' === $this->recipient) {
            return false;
        }

        return $this->send(
            $this->get_recipient(),
            $composed['subject'],
            $this->get_content(),
            $this->get_headers(),
            $this->get_attachments()
        );
    }

    /**
     * @param mixed $request
     *
     * @return array{subject:string,html:string,text:string}
     */
    protected function build(
        EmailComposer $composer,
        $request,
        string $reference,
        string $localTime,
        string $timezone
    ): array {
        return $composer->clientConfirmation($request, $reference, $localTime, $timezone, '');
    }
}
