<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

use App\Casts\CompressedString;
use App\Classes\FTN\Message;
use App\Interfaces\Packet;
use App\Traits\{EncodeUTF8,MsgID};

final class Netmail extends Model implements Packet
{
	private const LOGKEY = 'MN-';

	use SoftDeletes,EncodeUTF8,MsgID;

	private Collection $set_path;
	private Address $set_sender;
	private Carbon $set_recvtime;
	private string $set_pkt;

	private const cast_utf8 = [
		'to',
		'from',
		'subject',
		'msg',
		'msg_src',
		'origin',
		'tearline',
		'tagline',
	];

	protected $casts = [
		'datetime' => 'datetime:Y-m-d H:i:s',
		'msg' => CompressedString::class,
		'msg_src' => CompressedString::class,
		'sent_at' => 'datetime:Y-m-d H:i:s',
	];

	public function __set($key,$value)
	{
		switch ($key) {
			case 'set_path':
			case 'set_pkt':
			case 'set_recvtime':
			case 'set_sender':
				$this->{$key} = $value;
				break;

			default:
				parent::__set($key,$value);
		}
	}

	public static function boot()
	{
		parent::boot();

		static::created(function($model) {
			$nodes = collect();

			// Parse PATH
			// <FTN Address> @YYYYMMDD.HHMMSS[.Precise][.Time Zone] <Program Name> <Version> [Serial Number]
			if (isset($model->set_path)) {
				if ($model->set_path->count()) {
					foreach ($model->set_path as $line) {
						$m = [];

						if (preg_match('/^([0-9]+:[0-9]+\/[0-9]+(\..*)?)\s+@([0-9.a-zA-Z]+)\s+(.*)$/',$line,$m)) {
							// Address
							$ao = Address::findFTN($m[1]);

							// Time
							$t = [];
							$datetime = '';

							if (! preg_match('/^([0-9]+\.[0-9]+)(\.?(.*))?$/',$m[3],$t))
								Log::alert(sprintf('%s:! Unable to determine time from [%s]',self::LOGKEY,$m[3]));
							else
								$datetime = Carbon::createFromFormat('Ymd.His',$t[1],$t[3] ?? '');

							if (! $ao) {
								Log::alert(sprintf('%s:! Undefined Node [%s] for Netmail.',self::LOGKEY,$m[1]));
								//$rogue->push(['node'=>$m[1],'datetime'=>$datetime,'program'=>$m[4]]);

							} else {
								$nodes->push(['node'=>$ao,'datetime'=>$datetime,'program'=>$m[4]]);
							}
						}
					}

				// If there are no details (Mystic), we'll create a blank
				} else {
					$nodes->push(['node'=>$model->set_sender,'datetime'=>Carbon::now(),'program'=>'Unknown']);
				}
			}

			// Save the Path
			$ppoid = NULL;

			foreach ($nodes as $path) {
				$po = DB::select('INSERT INTO netmail_path (netmail_id,address_id,parent_id,datetime,program) VALUES (?,?,?,?,?) RETURNING id',[
					$model->id,
					$path['node']->id,
					$ppoid,
					(string)$path['datetime'],
					$path['program'],
				]);

				$ppoid = $po[0]->id;
			}

			// Our last node in the path is our sender
			if ($nodes->count() && isset($model->set_pkt) && isset($model->set_sender) && isset($model->set_recvtime)) {
				DB::update('UPDATE netmail_path set recv_pkt=?,recv_at=?,recv_id=? where address_id=? and netmail_id=?',[
					$model->set_pkt,
					$model->set_recvtime,
					$model->set_sender->id,
					Arr::get($nodes->last(),'node')->id,
					$model->id,
				]);
			}
		});
	}

	/* RELATIONS */

	public function fftn()
	{
		return $this
			->belongsTo(Address::class)
			->withTrashed();
	}

	public function path()
	{
		return $this->belongsToMany(Address::class,'netmail_path')
			->withPivot(['id','parent_id','datetime','program','recv_pkt','recv_id']);
	}

	public function received()
	{
		return $this->belongsToMany(Address::class,'netmail_path','netmail_id','recv_id')
			->withPivot(['id','parent_id','datetime','program','recv_pkt','recv_id']);
	}

	public function tftn()
	{
		return $this
			->belongsTo(Address::class);
	}

	/* METHODS */

	/**
	 * Return this model as a packet
	 */
	public function packet(Address $ao,string $strippass=NULL): Message
	{
		Log::debug(sprintf('%s:+ Bundling [%s]',self::LOGKEY,$this->id));

		// @todo Dont bundle mail to nodes that have been disabled, or addresses that have been deleted
		$o = new Message;

		try {
			$o->header = [
				'onode' => $this->fftn->node_id,
				'dnode' => $ao->node_id,
				'onet' => $this->fftn->host_id,
				'dnet' => $ao->host_id,
				'opoint' => $this->fftn->point_id,
				'dpoint' => $ao->point_id,
				'flags' => 0,
				'cost' => 0,
				'date'=>$this->datetime->format('d M y  H:i:s'),
			];

			$o->tzutc = $this->datetime->utcOffset($this->tzoffset)->getOffsetString('');
			$o->user_to = $this->to;
			$o->user_from = $this->from;
			$o->subject = (! is_null($strippass)) ? preg_replace('/^'.$strippass.':/','',$this->subject) : $this->subject;

			// INTL kludge
			$o->intl = sprintf('%s %s',$this->tftn->ftn3d,$this->fftn->ftn3d);
			$o->flags = $this->flags;

			$o->msgid = sprintf('%s %08x',$this->fftn->ftn3d,crc32($this->id));
			if ($this->replyid)
				$o->replyid = $this->replyid;

			$o->kludge->put('dbid',$this->id);

			$o->message = $this->msg;
			$o->tagline = $this->tagline;
			$o->tearline = $this->tearline;

			// VIA kludge
			$sysaddress = Setup::findOrFail(config('app.id'))->system->match($this->fftn->zone)->first();
			$via = $this->via ?: collect();
			// Add our address to the VIA line
			$via->push(
				sprintf('%s @%s.UTC %s %d.%d/%s %s',
					$sysaddress->ftn3d,
					Carbon::now()->utc()->format('Ymd.His'),
					str_replace(' ','_',Setup::PRODUCT_NAME),
					Setup::PRODUCT_VERSION_MAJ,
					Setup::PRODUCT_VERSION_MIN,
					(new Setup)->version,
					Carbon::now()->format('Y-m-d'),
			));

			$o->via = $via;

			$o->packed = TRUE;

		} catch (\Exception $e) {
			Log::error(sprintf('%s:! Error converting netmail [%s] to a message (%d:%s)',self::LOGKEY,$this->id,$e->getLine(),$e->getMessage()));
			dump($this);
		}

		return $o;
	}

	public function pathorder(string $display='ftn2d',int $start=NULL): Collection
	{
		$result = collect();

		if ($x=$this->path->firstWhere('pivot.parent_id',$start)) {
			$result->push($x->$display);
			$result->push($this->pathorder($display,$x->pivot->id));
		}

		return $result->flatten()->filter();
	}
}