<?php

namespace Database\Seeders;

use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

use App\Models\{Address, Domain, Setup, System, Zone};

class TestNodeHierarchy extends Seeder
{
	public const DEBUG=FALSE;

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
		DB::table('domains')
			->insert([
				'name'=>'a',
				'active'=>TRUE,
				'public'=>TRUE,
				'created_at'=>Carbon::now(),
				'updated_at'=>Carbon::now(),
			]);

		DB::table('domains')
			->insert([
				'name'=>'b',
				'active'=>TRUE,
				'public'=>TRUE,
				'created_at'=>Carbon::now(),
				'updated_at'=>Carbon::now(),
			]);

		foreach (['a','b'] as $domain) {
			$do = Domain::where('name',$domain)->sole();
			$this->hierarchy($do,100);
			$this->hierarchy($do,101);
		}

		// Configure my addresses
		$so = Setup::findOrFail(config('app.id'));
		$ao = Address::findFTN('100:0/0@a');
		$so->system_id = $ao->system_id;
		$so->save();

		// Add file area
		DB::table('fileareas')
			->insert([
				'name'=>'FILE_AREA',
				'description'=>'Testing File Area',
				'active'=>TRUE,
				'show'=>TRUE,
				'notes'=>'File area for testing',
				'domain_id'=>$ao->zone->domain_id,
				'security'=>1,
				'created_at'=>Carbon::now(),
				'updated_at'=>Carbon::now(),
			]);
    }

    private function hierarchy(Domain $domain,int $zoneid)
	{
		$hosts = [1,2,3,4,5];
		$hubs = [10,20,30,40,50];
		$nodes = [100,200,300,400,500];
		$hubnodes = [-2,-1,+1,+2,+3];

		$so = $this->system(sprintf('ZC %s-%s',$domain->name,$zoneid));
		DB::table('zones')
			->insert([
				'zone_id'=>$zoneid,
				'active'=>TRUE,
				'default'=>$domain->name === 'a',
				'notes'=>sprintf('Zone: %03d:0/0.0@%s',$zoneid,$domain->name),
				'domain_id'=>$domain->id,
				'system_id'=>$so->id,
				'created_at'=>Carbon::now(),
				'updated_at'=>Carbon::now(),
			]);

		$zo = Zone::where('zone_id',$zoneid)->where('domain_id',$domain->id)->sole();
		if (self::DEBUG)
			dump(['zo'=>$zo->zone_id,'rid'=>0,'hid'=>0,'nid'=>0]);

		// ZC
		DB::table('addresses')
			->insert([
				'zone_id'=>$zo->id,
				'active'=>TRUE,
				'validated'=>TRUE,
				'region_id'=>0,
				'host_id'=>0,
				'node_id'=>0,
				'point_id'=>0,
				'system_id'=>$so->id,
				'role'=>Address::NODE_ZC,
				'created_at'=>Carbon::now(),
				'updated_at'=>Carbon::now(),
			]);

		// ZC Nodes
		foreach ($nodes as $nid) {
			if (self::DEBUG)
				dump(['rid'=>$zo->zone_id,'hid'=>$zo->zone_id,'nid'=>$nid]);

			$so = $this->system(sprintf('ZC Node 0/%d',$nid));
			DB::table('addresses')
				->insert([
					'zone_id'=>$zo->id,
					'active'=>TRUE,
					'validated'=>TRUE,
					'region_id'=>0,
					'host_id'=>0,
					'node_id'=>$nid,
					'point_id'=>0,
					'system_id'=>$so->id,
					'role'=>Address::NODE_NN,
					'created_at'=>Carbon::now(),
					'updated_at'=>Carbon::now(),
				]);
		}

		if (self::DEBUG)
			dump(['end'=>'nodes top']);

		// Regions
		foreach ($hosts as $rid) {
			$hostid = $rid;
			if (self::DEBUG)
				dump(['rid'=>$rid,'hid'=>$hostid,'nid'=>0]);

			$so = $this->system(sprintf('Region %03d:%03d/%03d.0@%s',$zoneid,$rid,0,$domain->name));
			DB::table('addresses')
				->insert([
					'zone_id'=>$zo->id,
					'active'=>TRUE,
					'validated'=>TRUE,
					'region_id'=>$rid,
					'host_id'=>$rid,
					'node_id'=>0,
					'point_id'=>0,
					'system_id'=>$so->id,
					'role'=>Address::NODE_RC,
					'created_at'=>Carbon::now(),
					'updated_at'=>Carbon::now(),
				]);

			// RC Nodes
			foreach ($nodes as $nid) {
				if (self::DEBUG)
					dump(['rid'=>$rid,'hid'=>$hostid,'nid'=>$nid]);

				$so = $this->system(sprintf('RC Node %d/%d',$rid,$nid));
				DB::table('addresses')
					->insert([
						'zone_id'=>$zo->id,
						'active'=>TRUE,
						'validated'=>TRUE,
						'region_id'=>$rid,
						'host_id'=>$rid,
						'node_id'=>$nid,
						'point_id'=>0,
						'system_id'=>$so->id,
						'role'=>Address::NODE_NN,
						'created_at'=>Carbon::now(),
						'updated_at'=>Carbon::now(),
					]);
			}
			if (self::DEBUG)
				dump(['end'=>'NODES regions']);

			// Hosts
			foreach ($hosts as $rrid) {
				$hostid = $rid*10+$rrid-1;
				if (self::DEBUG)
					dump(['rid'=>$rid,'hid'=>$hostid,'nid'=>0]);

				$so = $this->system(sprintf('Host %d:%d/0 (R%d)',$zoneid,$hostid,$rid));

				DB::table('addresses')
					->insert([
						'zone_id'=>$zo->id,
						'active'=>TRUE,
						'validated'=>TRUE,
						'region_id'=>$rid,
						'host_id'=>$hostid,
						'node_id'=>0,
						'point_id'=>0,
						'system_id'=>$so->id,
						'role'=>Address::NODE_NC,
						'created_at'=>Carbon::now(),
						'updated_at'=>Carbon::now(),
					]);

				// Nodes
				foreach ($nodes as $nid) {
					if (self::DEBUG)
						dump(['rid'=>$rid,'hid'=>$hostid,'nid'=>$nid]);

					$so = $this->system(sprintf('Host Node %d/%d (R%d)',$hostid,$nid,$rid));
					DB::table('addresses')
						->insert([
							'zone_id'=>$zo->id,
							'active'=>TRUE,
							'validated'=>TRUE,
							'region_id'=>$rid,
							'host_id'=>$hostid,
							'node_id'=>$nid,
							'point_id'=>0,
							'system_id'=>$so->id,
							'role'=>Address::NODE_NN,
							'created_at'=>Carbon::now(),
							'updated_at'=>Carbon::now(),
						]);
				}

				// Hubs
				foreach ($hubs as $bid) {
					$so = $this->system(sprintf('HUB %d/%d (R%d)',$hostid,$bid,$rid));
					$hub = new Address;
					$hub->zone_id = $zo->id;
					$hub->active = TRUE;
					$hub->region_id = $rid;
					$hub->host_id = $hostid;
					$hub->node_id = $bid;
					$hub->point_id = 0;
					$hub->system_id = $so->id;
					$hub->role = Address::NODE_HC;
					$hub->created_at = Carbon::now();
					$hub->updated_at = Carbon::now();
					$hub->save();

					// Nodes
					foreach ($hubnodes as $nid) {
						$nodeid = $bid+$nid;
						$so = $this->system(sprintf('Hub Node %d/%d (R%d/H%d)',$hostid,$nodeid,$rid,$hub->node_id));
						DB::table('addresses')
							->insert([
								'zone_id'=>$zo->id,
								'active'=>TRUE,
								'validated'=>TRUE,
								'region_id'=>$rid,
								'host_id'=>$hostid,
								'node_id'=>$nodeid,
								'point_id'=>0,
								'system_id'=>$so->id,
								'hub_id'=>$hub->id,
								'role'=>Address::NODE_NN,
								'created_at'=>Carbon::now(),
								'updated_at'=>Carbon::now(),
							]);
					}
				}
			}
			if (self::DEBUG)
				dump(['end'=>'NODES normal']);
		}

		if (self::DEBUG)
			dump(['end'=>'heirarchy']);
	}

	private function system(string $name): System
	{
		$o = new System;
		$o->name = $name;
		$o->sysop = 'Sysop:'.$name;
		$o->location = 'Location:'.$name;
		$o->active = TRUE;
		$o->created_at = Carbon::now();
		$o->updated_at = Carbon::now();
		$o->save();

		return $o;
	}
}