<?php

namespace App\Notifications\Netmails;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Support\Facades\Log;

use App\Classes\FTN\Message;
use App\Notifications\Netmails;
use App\Models\Netmail;
use App\Traits\{MessagePath,PageTemplate};

class Ping extends Netmails
{
	use MessagePath,PageTemplate;

	private const LOGKEY = 'NNP';

	private Netmail $mo;

	/**
	 * Reply to a netmail ping request.
	 *
	 * @param Message $mo
	 */
	public function __construct(Netmail $mo)
	{
		parent::__construct();

		$this->mo = $mo;
	}

	/**
	 * Get the mail representation of the notification.
	 *
	 * @param mixed $notifiable
	 * @return Netmail
	 * @throws \Exception
	 */
	public function toNetmail(object $notifiable): Netmail
	{
		$o = $this->setupNetmail($notifiable);
		$ao = $notifiable->routeNotificationFor(static::via);

		Log::info(sprintf('%s:+ Creating PING netmail to [%s]',self::LOGKEY,$ao->ftn));

		$o->to = $this->mo->from;
		$o->replyid = $this->mo->msgid;
		$o->subject = 'Ping Reply';

		// Message
		$msg = $this->page(FALSE,'Ping');

		$msg->addText(
			sprintf("Your ping was received here on %s and it looks like you sent it on %s. If that is correct, then it took %s to get here.\r\r",
				Carbon::now()->utc()->toDateTimeString(),
				$this->mo->date->utc()->toDateTimeString(),
				$this->mo->date->diffForHumans(['parts'=>3,'syntax'=>CarbonInterface::DIFF_ABSOLUTE])
			)
		);

		$msg->addText($this->message_path($this->mo));

		$o->msg = $msg->render();
		$o->tagline = 'My ping pong opponent was not happy with my serve. He kept returning it.';

		$o->save();

		return $o;
	}
}