<?php

namespace App\Console\Commands;

use App\Models\Address;
use Carbon\Carbon;
use Illuminate\Console\Command;

use App\Jobs\NodesNew as Job;
use App\Models\Domain;

class NodesNew extends Command
{
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'nodes:new'
		.' {domain : Domain}'
		.' {--date= : From a specific date (default 1 since last Saturday)}'
		.' {--netmail= : Send a Netmail to FTN}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'List new nodes since last Saturday (or a specific date)';

	/**
	 * Execute the console command.
	 */
	public function handle(): int
	{
		$do = Domain::where('name',$this->argument('domain'))->singleOrFail();
		$ao = NULL;

		if ($this->option('netmail')) {
			$ao = Address::findFTN($this->option('netmail'));

			if (! $ao) {
				$this->error('Address not found: '.$this->option('netmail'));
				return self::FAILURE;
			}
		}

		return Job::dispatchSync($do,$this->option('date') ? Carbon::parse($this->option('date')) : Carbon::parse('last saturday'),$ao);
	}
}