<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Jenssegers\Mongodb\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;

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

final class Echomail extends Model implements Packet
{
	use SoftDeletes,MsgID,UseMongo,EncodeUTF8;

	private const LOGKEY = 'ME-';

	protected $collection = FALSE;

	protected $casts = [ 'kludges' => 'json' ];

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

	protected $dates = ['datetime'];

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

		// @todo if the message is updated with new SEEN-BY's from another route, we'll delete the pending export for systems (if there is one)
		static::created(function($model) {
			if (! $model->echoarea_id) {
				Log::alert(sprintf('%s:- Message has no echo area, not exporting',self::LOGKEY,$model->id));
				return;
			}

			// See if we need to export this message.
			$exportto = $model->echoarea->addresses->pluck('system')->diff($model->seenby->pluck('system'));

			$export_ao = collect();
			foreach ($model->echoarea->domain->zones as $zo) {
				foreach ($exportto as $so) {
					$export_ao = $export_ao->merge($so->match($zo));
				}
			}

			// Add to export
			foreach ($export_ao as $ao) {
				Log::info(sprintf('%s:- Exporting message [%s] to [%s]',self::LOGKEY,$model->id,$ao->ftn));

				DB::table('address_echomail')->insert([
					'address_id'=>$ao->id,
					'echomail_id'=>$model->id,
					'export_date'=>Carbon::now()
				]);
			}

			$model->seenby = $model->seenby->merge($export_ao)->pluck('id')->toArray();
			$model->save();
		});
	}

	/* RELATIONS */

	public function echoarea()
	{
		return $this->belongsTo(Echoarea::class);
	}

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

	/* ATTRIBUTES */

	public function getKludgesAttribute(?string $value): Collection
	{
		return collect($this->castAttribute('kludges',$value));
	}

	public function getPathAttribute(?array $value): Collection
	{
		if (is_null($value))
			return collect();

		return Address::whereIn('id',$value)
			->orderBy(DB::raw(sprintf("position (id::text in '(%s)')",join(',',$value))))
			->get();
	}

	public function getSeenByAttribute(?array $value): Collection
	{
		if (is_null($value))
			return collect();

		return Address::whereIn('id',$value)->get();
	}

	/* METHODS */

	public function jsonSerialize(): array
	{
		return $this->encode();
	}

	/**
	 * Return this model as a packet
	 */
	public function packet(Address $ao): 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;

		$o->header = [
			'onode' => $this->fftn->node_id,
			'dnode' => $ao->node_id,
			'onet' => $this->fftn->host_id,
			'dnet' => $ao->host_id,
			'flags' => 0,	// @todo?
			'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 = $this->subject;
		$o->echoarea = $this->echoarea->name;
		$o->flags = $this->flags;

		if ($this->kludges)
			$o->kludge = $this->kludges;

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

		$o->msgid = $this->msgid;
		if ($this->reply)
			$o->reply = $this->reply;

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

		if ($this->tagline)
			$o->tagline = $this->tagline;

		if ($this->tearline)
			$o->tearline = $this->tearline;

		if ($this->origin)
			$o->origin = $this->origin;

		// @todo SEENBY
		// @todo PATH

		$o->packed = TRUE;

		return $o;
	}
}