<?php

namespace App\Notifications\Netmails;

use Carbon\Carbon;
use Illuminate\Bus\Queueable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Log;

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

class NodesNew extends Netmails //implements ShouldQueue
{
	use Queueable,PageTemplate;

	private const LOGKEY = 'NNN';

	private Carbon $since;
	private Collection $list;

	/**
	 * Create a new notification instance.
	 */
	public function __construct(Carbon $since,Collection $list)
	{
		parent::__construct();

		$this->list = $list;
		$this->since = $since;
	}

	public function toNetmail(object $notifiable): Netmail
	{
		$o = $this->setupNetmail($notifiable);
		$ao = $notifiable->routeNotificationFor(static::via);

		Log::info(sprintf('%s:+ Sending a NEW NODE LIST to [%s] at address [%s]',self::LOGKEY,$ao->system->sysop,$ao->ftn));

		$o->subject = sprintf('Here is a list of new nodes on clrghouz since %s',$this->since->format('Y-m-d'));
		$o->flags = (Message::FLAG_LOCAL|Message::FLAG_PRIVATE|Message::FLAG_CRASH);

		$o->msg = $this->report($ao);
		$o->set_tagline = 'All aboard!';

		$o->save();

		return $o;
	}

	/**
	 * Return the rendered new nodes report.
	 */
	public function report(Address $ao): string
	{
		// Message
		$msg = $this->page(FALSE,'new nodes');

		$msg->addText(sprintf("Hi %s,\r\r",$ao->system->sysop))
			->addText(sprintf("The following new system have been defined on clrghouz since %s.\r\r",$this->since->format('Y-m-d')));

		$this->list->loadMissing(['system']);

		$space = str_repeat(' ',$this->list->pluck('ftn4d')->max(fn($item)=>strlen($item))+2);

		$c = 0;
		foreach ($this->list as $oo) {
			if ($c++)
				$msg->addText("\r");

			$msg->addText(sprintf("* %s - %s (%s) from %s.\r",$oo->ftn4D,$oo->system->sysop,$oo->system->name,$oo->system->location));
			$msg->addText(sprintf("%s   Address registered: %s\r\r",$space,$oo->created_at->format('Y-m-d')));

			if ($oo->system->method) {
				switch ($oo->system->method) {
					case 23:
						$msg->addText(sprintf("%s - BBS is available TELNET [%s:%d]\r",$space,$oo->system->address,$oo->system->port));
						break;

					case 22:
						$msg->addText(sprintf("%s - BBS is available SSH [%s:%d]\r",$space,$oo->system->address,$oo->system->port));
						break;

					case 519:
						$msg->addText(sprintf("%s - BBS is available RLOGIN [%s:%d]\r",$space,$oo->system->address,$oo->system->port));
						break;

					default:
						$msg->addText(sprintf("%s - No Details available for connecting to BBS\r",$space));
				}
			}

			if ($oo->system->mailers->count()) {
				$msg->addText("\r");
				$msg->addText(sprintf("%s - Mail can be sent using:\r",$space));

				foreach ($oo->system->mailers as $mo) {
					$msg->addText(sprintf("%s   * %s [%s:%d]\r",$space,$mo->name,$oo->system->address,$mo->pivot->port));
				}

			} else {
				$msg->addText(sprintf("%s - No mailer information provided, so will be PRIVATE\r",$space));
			}
		}

		return $msg->render();
	}
}