<?php

namespace App\Classes\FTN\Process\Netmail;

use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Log;

use App\Classes\FTN\Process;
use App\Models\{Echomail,Netmail};
use App\Notifications\Netmails\Areafix\{InvalidPassword,NotConfiguredHere};

/**
 * Process messages to Ping
 *
 * @package App\Classes\FTN\Process
 */
abstract class Robot extends Process
{
	private const LOGKEY = 'RPR';

	public static function handle(Echomail|Netmail $mo): bool
	{
		if (((strtolower($mo->to) !== 'areafix') && (strtolower($mo->to) !== 'filefix')) || (! ($mo instanceof Netmail)))
			return FALSE;

		Log::info(sprintf('%s:- Processing *FIX [%s] message from (%s) [%s]',self::LOGKEY,$mo->to,$mo->from,$mo->fftn->ftn));

		// If this is not a node we manage, then respond with a sorry can help you
		if (! $mo->fftn->system->sessions->count()) {
			Notification::route('netmail',$mo->fftn)->notify(new NotConfiguredHere($mo));

			return TRUE;
		}

		// If this nodes password is not correct
		if ($mo->fftn->pass_fix !== strtoupper($mo->subject)) {
			Notification::route('netmail',$mo->fftn)->notify(new InvalidPassword($mo));

			return TRUE;
		}

		if ((strtolower($mo->to) === 'areafix'))
			return static::areafix($mo);

		if ((strtolower($mo->to) === 'filefix'))
			return static::filefix($mo);

		return FALSE;
	}
}