<?php

namespace App\Notifications\Echomails;

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

use App\Classes\{Fonts\Thick,Fonts\Thin,Page};
use App\Models\{Echomail,Setup};
use App\Notifications\Echomails;
use App\Traits\MessagePath;

class Test extends Echomails
{
	use MessagePath;

	private const LOGKEY = 'NNP';

	/**
	 * Respond to a test in echomail.
	 *
	 * @param Echomail $mo
	 */
	public function __construct(private Echomail $mo)
	{
		parent::__construct();
	}

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

		Log::info(sprintf('%s:+ Creating TEST echomail in [%s]',self::LOGKEY,$echoarea->name));

		$our = our_address($this->mo->fftn);
		$o->to = $this->mo->from;
		$o->fftn_id = $our->id;
		$o->replyid = $this->mo->msgid;
		$o->subject = 'Test Reply';
		$o->kludges->put('CHRS:',$this->mo->kludges->get('chrs') ?: 'CP437 2');

		// Message
		$msg = new Page;

		$header = new Thick;
		$header->addText('Clearing Houz');
		$msg->addHeader($header,'FTN Mailer and Tosser',TRUE,0xc4);

		$lbc = new Thin;
		$lbc->addText('Test');
		$msg->addLeftBoxContent($lbc,TRUE);

		$msg->addText(
			sprintf("Your test 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->set_tagline = 'I ate a clock yesterday, it was very time-consuming.';
		$o->set_origin = sprintf('%s (%s)',Setup::PRODUCT_NAME,$our->ftn4d);

		$o->save();

		return $o;
	}
}