<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\{Address,System,Zone};
use App\Rules\{FidoInteger,TwoByteInteger};

class SystemController extends Controller
{
	public function __construct()
	{
		$this->middleware('auth');
	}

	/**
	 * Add an address to a system
	 *
	 * @param Request $request
	 * @param System $o
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function add_address(Request $request,System $o)
	{
		// @todo This should be admin of the zone
		$this->authorize('admin',$o);
		session()->flash('add_address',TRUE);

		$request->validate([
			'action' => 'required|in:region,host,node',
			'zone_id' => 'required|exists:zones,id',
		]);

		switch ($request->post('action')) {
			case 'region':
				$request->validate([
					'region_id_new' => [
						'required',
						new TwoByteInteger,
						function ($attribute,$value,$fail) {
							// Check that the region doesnt already exist
							$o = Address::where(function($query) use ($value) {
								return $query->where('region_id',$value)
									->where('host_id',0)
									->where('node_id',0)
									->where('point_id',0)
									->where('role',DomainController::NODE_RC);
							})
							// Check that a host doesnt already exist
							->orWhere(function($query) use ($value) {
								return $query->where('host_id',$value)
									->where('point_id',0)
									->where('role',DomainController::NODE_NC);
							});

							if ($o->count()) {
								$fail('Region or host already exists');
							}
						},
					],
				]);

				$oo = new Address;
				$oo->zone_id = $request->post('zone_id');
				$oo->region_id = $request->post('region_id_new');
				$oo->node_id = 0;
				$oo->point_id = 0;
				$oo->role = DomainController::NODE_RC;
				$oo->active = TRUE;

				$o->addresses()->save($oo);
				break;

			case 'host':
				$request->validate([
					'region_id' => ['required',new FidoInteger],
					'host_id_new' => [
						'required',
						new TwoByteInteger,
						function ($attribute,$value,$fail) use ($request) {
							// Check that the region doesnt already exist
							$o = Address::where(function($query) use ($value) {
								return $query->where('region_id',$value)
									->where('role',DomainController::NODE_RC);
							})
							// Check that a host doesnt already exist
							->orWhere(function($query) use ($value) {
								return $query->where('host_id',$value)
									->where('role',DomainController::NODE_NC);
							})
							->where('zone_id',$request->post('zone_id'))
							->where('point_id',0)
							->where('active',TRUE);

							if ($o->count()) {
								$fail('Region or host already exists');
							}
						},
					],
					'node_id_new' => [
						'required',
						new TwoByteInteger,
						function ($attribute,$value,$fail) use ($request) {
							// Check that the region doesnt already exist
							$o = Address::where(function($query) use ($request,$value) {
								return $query
									->where('host_id',$request->post('host_id_new'))
									->where('node_id',$value)
									->where('point_id',0)
									->where('role',DomainController::NODE_RC);
							});

							if ($o->count()) {
								$fail('Host already exists');
							}
						},
					]
				]);

				$oo = new Address;
				$oo->zone_id = $request->post('zone_id');
				$oo->region_id = $request->post('region_id');
				$oo->host_id = $request->post('host_id_new');
				$oo->node_id = $request->post('node_id_new');
				$oo->point_id = 0;
				$oo->role = DomainController::NODE_NC;
				$oo->active = TRUE;

				$o->addresses()->save($oo);
				break;

			case 'node':
				$request->validate([
					'region_id' => ['required',new FidoInteger],
					'host_id' => ['required',new FidoInteger],
					'node_id' => [
						'required',
						new TwoByteInteger,
						function ($attribute,$value,$fail) use ($request) {
							// Check that the region doesnt already exist
							$o = Address::where(function($query) use ($request,$value) {
								return $query
									->where('host_id',$request->post('host_id'))
									->where('node_id',$value)
									->where('point_id',0)
									->where('role',DomainController::NODE_RC);
							});

							if ($o->count()) {
								$fail('Host already exists');
							}
						},
					],
					'point_id' => ['required',function($attribute,$value,$fail) {
						if (! is_numeric($value) || $value > DomainController::NUMBER_MAX)
							$fail(sprintf('Point numbers must be between 0 and %d',DomainController::NUMBER_MAX));
					}],
					'hub' => 'required|boolean',
					'hub_id' => 'nullable|exists:addresses,id',
				]);

				$oo = new Address;
				$oo->zone_id = $request->post('zone_id');
				$oo->region_id = $request->post('region_id');
				$oo->host_id = $request->post('host_id');
				$oo->node_id = $request->post('node_id');
				$oo->point_id = $request->post('point_id');
				$oo->hub_id = $request->post('hub_id') > 0 ? $request->post('hub_id') : NULL;
				$oo->role = (! $oo->point_id) && $request->post('hub') ? DomainController::NODE_HC : NULL;
				$oo->active = TRUE;

				$o->addresses()->save($oo);
				break;

			default:
				return redirect()->back()->withErrors(['action'=>'Unknown action: '.$request->post('action')]);
		}

		return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
	}

	/**
	 * Add Session details
	 *
	 * @param Request $request
	 * @param System $o
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function add_session(Request $request,System $o)
	{
		// @todo This should be admin of the zone
		$this->authorize('admin',$o);
		session()->flash('add_session',TRUE);

		$validate = $request->validate([
			'zone_id' => 'required|exists:zones,id',
			'sespass' => 'required|string|min:4',
			'pktpass' => 'required|string|min:4|max:8',
			'ticpass' => 'required|string|min:4',
			'fixpass' => 'required|string|min:4',
		]);

		$zo = Zone::findOrFail($validate['zone_id']);

		$o->sessions()->attach($zo,$validate);

		return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
	}

	/**
	 * Add or edit a node
	 */
	public function add_edit(Request $request,System $o)
	{
		if ($request->post()) {
			$this->authorize('admin',$o);

			$request->validate([
				'name' => 'required|min:3',
				'location' => 'required|min:3',
				'sysop' => 'required|min:3',
				'address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i',
				'port' => 'nullable|digits_between:2,5',
				'method' => 'nullable|numeric',
				'mailer_type' => 'nullable|numeric',
				'mailer_address' => 'nullable|regex:/^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$/i',
				'mailer_port' => 'nullable|digits_between:2,5',
				'active' => 'required|boolean',
				'zt_id' => 'nullable|size:10|regex:/^([A-Fa-f0-9]){10}$/|unique:systems,zt_id,'.($o->exists ? $o->id : 0),
			]);

			foreach (['name','location','sysop','address','port','active','method','notes','mailer_type','mailer_address','mailer_port','zt_id'] as $key)
				$o->{$key} = $request->post($key);

			$o->save();

			return redirect()->action([self::class,'home']);
		}

		return view('system.addedit')
			->with('o',$o);
	}

	/**
	 * Delete address assigned to a host
	 *
	 * @param Address $o
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function del_address(Address $o)
	{
		// @todo This should be admin of the zone
		$this->authorize('admin',$o);
		session()->flash('add_address',TRUE);

		$sid = $o->system_id;
		$o->active = FALSE;
		$o->save();
		$o->delete();

		return redirect()->to(sprintf('ftn/system/addedit/%d',$sid));
	}

	/**
	 * Delete address assigned to a host
	 *
	 * @param Address $o
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function del_session(System $o,Zone $zo)
	{
		$this->authorize('admin',$zo);
		session()->flash('add_session',TRUE);

		$o->sessions()->detach($zo);

		return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
	}

	public function ours()
	{
		return view('system.ours');
	}

	/**
	 * Suspend address assigned to a host
	 *
	 * @param Address $o
	 * @return \Illuminate\Http\RedirectResponse
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function sus_address(Address $o)
	{
		// @todo This should be admin of the zone
		$this->authorize('admin',$o);
		session()->flash('add_address',TRUE);

		$o->active = (! $o->active);
		$o->save();

		return redirect()->to(sprintf('ftn/system/addedit/%d',$o->system_id));
	}

	public function home()
	{
		return view('system.home');
	}
}