<?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,System};
use App\Traits\{MessagePath,PageTemplate};

class Ping extends Netmails
{
	use MessagePath,PageTemplate;

	private const LOGKEY = 'NNP';

	private Message $mo;

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

		$this->mo = $mo;
	}

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

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

		$o->to = $this->mo->user_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;
	}
}