<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\System;

class PacketSystem extends Command
{
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'packet:system'
		.' {sid : System ID}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Show mail waiting for a system';

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 * @throws \App\Classes\FTN\InvalidPacketException
	 */
	public function handle()
	{
		$so = System::findOrFail($this->argument('sid'));

		foreach ($so->addresses as $ao) {
			$pkt = $ao->getEchomail(FALSE);
			$this->info(sprintf('System address [%s] has [%d] messages.',$ao->ftn,$pkt?->count()));

			if ($pkt) {
				foreach ($pkt as $msg)
					$this->warn(sprintf('- %s',$msg->msgid));
			}
		}
	}
}