<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;

use App\Jobs\SupplierDomainSync as Job;
use App\Models\{Site,Supplier};

class SupplierDomainSync extends Command
{
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'supplier:domain:sync'
		.' {supplier : Supplier Name}'
		.' {--f|forceprod : Force Prod API on dev environment}'
		.' {--s|site : Site ID}';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'Sync domains from a supplier';

	/**
	 * Execute the console command.
	 *
	 * @return int
	 */
	public function handle()
	{
		Config::set(
			'site',
			($x=$this->option('site')
				? Site::findOrFail($this->option('site'))
				: Site::where('url',config('app.url'))->sole())
		);

		$so = Supplier::where('name',$this->argument('supplier'))->singleOrFail();

		return Job::dispatchSync($x,$so,$this->option('forceprod'));
	}
}