<?php
/**
 * Notifies the shop that a withdrawal has been declared.
 *
 * @package SkyServers\Retragere
 */

declare(strict_types=1);

namespace SkyServers\Retragere\Wc\Emails;

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

/**
 * Never carries the access token: this message goes to shop staff, not to the
 * person the token belongs to.
 */
final class StoreEmail extends BaseEmail
{
    public function __construct()
    {
        $this->id          = 'ssrc_store_notification';
        $this->title       = __('Contract withdrawal notification', 'skyservers-retragere-contract');
        $this->description = __('The notice sent to the shop when a withdrawal request arrives.', 'skyservers-retragere-contract');
        $this->heading     = __('New withdrawal request', 'skyservers-retragere-contract');
        $this->subject     = __('New contract withdrawal request', 'skyservers-retragere-contract');

        parent::__construct();

        $this->recipient = (string) Settings::get('notify_recipients');
    }

    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) Settings::get('notify_recipients');

        if (!$this->is_enabled() || '' === trim($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->storeNotification($request, $reference, $localTime, $timezone);
    }
}
