<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Log;

use App\Models\{Address,Mailer};
use App\Jobs\AddressPoll as Job;

class CommEMSISend extends Command
{
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'comm:emsi:send {ftn : FTN to Send to}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'EMSI send';

	private const ID = 'EMSI';

	/**
	 * Execute the console command.
	 *
	 * @throws \Exception
	 */
	public function handle(): void
	{
		$ao = Address::findFTN($this->argument('ftn'));
		if (! $ao)
			throw new ModelNotFoundException('Unknown node: '.$this->argument('ftn'));

		Log::info(sprintf('CES:- Call EMSI send for %s',$ao->ftn));

		$mo = Mailer::where('name',self::ID)->singleOrFail();

		Job::dispatch($ao,$mo);
	}
}