<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

use App\Models\{Domain,Setup};

class HomeController extends Controller
{
	public function network(Domain $o)
	{
		return view('domain.view')
			->with('o',$o);
	}

	/**
	 * Render a view that summarises the users permissions
	 */
	public function permissions()
	{
		return view('auth.permissions')
			->with('user',Auth::user());
	}

	/**
	 * System Setup
	 *
	 * @note: Protected by Route
	 */
	public function setup(Request $request)
	{
		$o = Setup::findOrNew(config('app.id'));

		if ($request->post()) {
			$request->validate([
				'system_id' => 'required|exists:systems,id',
				'binkp' => 'nullable|array',
				'binkp.*' => 'nullable|numeric',
				'options' => 'nullable|array',
				'options.*' => 'nullable|numeric',
			]);

			if (! $o->exists) {
				$o->id = config('app.id');
				$o->zmodem = 0;
				$o->emsi_protocols = 0;
				$o->protocols = 0;
				$o->permissions = 0;
			}

			$o->binkp = collect($request->post('binkp'))->sum();
			$o->options = collect($request->post('options'))->sum();
			$o->system_id = $request->post('system_id');
			$o->save();
		}

		return view('setup')
			->with('o',$o);
	}
}