<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use Illuminate\Console\Command;

use App\Models\{Domain,Nodelist};
use App\Jobs\NodelistImport as Job;

class NodelistImport extends Command
{
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'nodelist:import'
		.' {domain : Domain Name}'
		.' {file : Nodelist File}'
		.' {--D|delete : Delete old data for the date}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Import Nodelist';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle()
	{
		$do = Domain::where('name',$this->argument('domain'))->singleOrFail();
		$o = Nodelist::firstOrCreate(['date'=>Carbon::now(),'domain_id'=>$do->id]);

		return Job::dispatchSync($do,$o,$this->argument('file'),$this->option('delete'));
	}
}