<?php

namespace App\Notifications\Echomails;

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

use App\Notifications\Echomails;
use App\Models\{Address,Echomail};
use App\Traits\PageTemplate;

class NodesNew extends Echomails //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 toEchomail(object $notifiable): Echomail
	{
		$echoarea = $notifiable->routeNotificationFor(static::via);
		$o = $this->setupEchomail($echoarea);

		Log::info(sprintf('%s:+ Sending a NEW NODE LIST to echoarea [%s]',self::LOGKEY,$echoarea->name));

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

		$our = our_address($echoarea->domain)->last();
		$o->to = 'All';
		$o->subject = 'New nodes to '.$echoarea->domain->name;
		$o->fftn_id = $our->id;
		$o->kludges->put('CHRS:','CP437 2');

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

		$o->save();

		return $o;
	}

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

		$msg->addText("Hi Everyone,\r\r")
			->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();
	}
}