<?php

declare(strict_types=1);

namespace Drupal\cron_fail_alert\Hook;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Mail\MailFormatHelper;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Hook implementations for Cron Fail Alert module.
 */
class CronFailAlertHooks {

  /**
   * Constructs CronFailAlertHooks.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   The config factory.
   */
  public function __construct(
    private readonly ConfigFactoryInterface $configFactory,
  ) {}

  /**
   * Implements hook_mail().
   */
  #[Hook('mail')]
  public function mail(string $key, array &$message, array $params): void {
    if ($key === 'cron_fail_alert_mail') {
      $options = ['langcode' => $message['langcode']];
      $message['from'] = $this->configFactory->get('system.site')->get('mail');
      $message['subject'] = new TranslatableMarkup('@title', ['@title' => $params['title']], $options);
      $message['body'][] = MailFormatHelper::wrapMail($params['message']);
    }
  }

}
