<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

use App\Models\{Address,Zone};

class ZoneController extends Controller
{
	/**
	 * Add or edit a zone
	 */
	public function add_edit(Request $request,Zone $o)
	{
		if ($request->post()) {
			$this->authorize('admin',$o);

			$request->validate([
				'domain_id' => 'required|exists:domains,id',
				'default' => [
					'required',
					'boolean',
					Rule::unique('zones')->where(function ($query) use ($request,$o) {
						return $query->where('zone_id',$request->post('zone_id'))
							->where('default',TRUE)
							->where('id','<>',$o->id);
					})
				],
				'zone_id' => [
					'required',
					'digits_between:1,5',
					Rule::unique('zones')->where(function ($query) use ($request,$o) {
						return $query->where('domain_id',$request->post('domain_id'))
							->when($o->exists,function($query) use ($o) {
								return $query->where('id','<>',$o->id);
							});
					})
				],
				'system_id' => 'required|exists:systems,id',
				'zt_id' => 'nullable|size:16|regex:/^([A-Fa-f0-9]){16}$/|unique:zones,zt_id,'.($o->exists ? $o->id : 0),
				'zt_ipv4' => 'nullable|ipv4',
				'zt_ipv4_mask' => [
					'nullable','numeric','lte:31','required_with:zt_ipv4',
					Rule::unique('zones')->where(function ($query) use ($request,$o) {
						return $query->where('zt_ipv4',$request->post('zt_ipv4'))
							->where('zt_ipv4_mask',$request->post('zt_ipv4_mask'))
							->when($o->exists,function($query) use ($o) {
								return $query->where('id','<>',$o->id);
							});
					})],
				'zt_ipv6' => 'nullable|ipv6',
				'zt_ipv6_mask' => [
					'nullable','numeric','lte:112','required_with:zt_ipv6',
					Rule::unique('zones')->where(function ($query) use ($request,$o) {
						return $query->where('zt_ipv6',$request->post('zt_ipv6'))
							->where('zt_ipv6_mask',$request->post('zt_ipv6_mask'))
							->when($o->exists,function($query) use ($o) {
								return $query->where('id','<>',$o->id);
							});
					})],
				'active' => 'required|boolean',
			]);

			foreach (['zone_id','default','domain_id','system_id','active','notes','zt_id','zt_ipv4','zt_ipv4_mask','zt_ipv6','zt_ipv6_mask'] as $key)
				$o->{$key} = $request->post($key);

			$o->save();
			$zo = Zone::where('zone_id',$request->zone_id)
				->where('domain_id',$request->domain_id)
				->sole();

			// Find the zones 0/0 address, and assign it to this host.
			$ao = Address::where('zone_id',$zo->id)
				->where('region_id',0)
				->where('host_id',0)
				->where('node_id',0)
				->where('point_id',0)
				->single();

			// Its not defined, so we'll create it.
			if (! $ao) {
				$ao = new Address;
				$ao->forceFill([
					'zone_id'=>$zo->id,
					'region_id'=>0,
					'host_id'=>0,
					'node_id'=>0,
					'point_id'=>0,
					'role'=>Address::NODE_ZC,
				]);
			}

			$ao->system_id = $request->system_id;
			$ao->active = TRUE;
			$o->addresses()->save($ao);

			return redirect()->to('zone');
		}

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

	/**
	 * Set address as default for zone
	 *
	 * @param Request $request
	 * @param Zone $o
	 * @throws \Illuminate\Auth\Access\AuthorizationException
	 */
	public function api_default(Request $request,Zone $o)
	{
		$this->authorize('admin',$o);

		$default = $o->systems->where('pivot.default',TRUE);

		if ($default->count() && $default->first()->addresses->pluck('role')->search(Address::NODE_ZC) !== FALSE)
			abort(412);

		if ($default->count() && ($default->first()->id != $request->sid))
			abort(409);

		$o->systems()->updateExistingPivot($request->sid,[
			'default' => (bool)$request->set,
		]);
	}
}