<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('supplier_broadband', function(Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$table->boolean('active')->default(false);

			$table->string('product_id',32);
			$table->text('product_desc')->nullable();

			$table->integer('metric')->nullable();
			$table->string('speed',32)->nullable();
			$table->string('technology')->nullable();

			$table->float('base_cost');
			$table->float('setup_cost')->nullable();
			$table->integer('contract_term')->unsigned()->nullable();
			$table->float('base_down_peak')->nullable();
			$table->float('base_up_peak')->nullable();
			$table->float('base_down_offpeak')->nullable();
			$table->float('base_up_offpeak')->nullable();
			$table->boolean('extra_charged')->default(false);
			$table->boolean('extra_shaped')->default(false);
			$table->float('extra_down_peak')->nullable();
			$table->float('extra_up_peak')->nullable();
			$table->float('extra_down_offpeak')->nullable();
			$table->float('extra_up_offpeak')->nullable();

			$table->time('offpeak_start')->nullable();
			$table->time('offpeak_end')->nullable();

			$table->integer('supplier_detail_id')->unsigned();
			$table->foreign(['supplier_detail_id','site_id'])->references(['id','site_id'])->on('supplier_details');

			$table->unique(['id','site_id']);
		});

		Schema::create('product_broadband', function(Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->integer('site_id')->unsigned();

			$table->integer('contract_term')->nullable();
			$table->float('base_down_peak', 10, 0)->nullable();
			$table->float('base_up_peak', 10, 0)->nullable();
			$table->float('base_down_offpeak', 10, 0)->nullable();
			$table->float('base_up_offpeak', 10, 0)->nullable();
			$table->boolean('extra_charged')->nullable();
			$table->string('extra_shaped', 8)->nullable();
			$table->float('extra_down_peak', 10, 0)->nullable();
			$table->float('extra_up_peak', 10, 0)->nullable();
			$table->float('extra_down_offpeak', 10, 0)->nullable();
			$table->float('extra_up_offpeak', 10, 0)->nullable();
			$table->float('metric', 10, 0)->default(0);

			$table->integer('supplier_item_id')->unsigned();
			$table->foreign(['supplier_item_id','site_id'])->references(['id','site_id'])->on('supplier_broadband');

			$table->index(['id','site_id']);
		});

		Schema::create('service_broadband', function(Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->integer('site_id')->unsigned();

			$table->string('service_number',16)->nullable();
			$table->string('service_address',256)->nullable();
			$table->string('service_username', 128)->nullable()->unique();
			$table->string('service_password', 16)->nullable();

			$table->boolean('service_stats_collect')->default(true);
			$table->date('service_stats_at')->nullable();

			$table->string('ipaddress', 45)->nullable();
			$table->time('offpeak_start')->nullable();
			$table->time('offpeak_end')->nullable();

			$table->date('connect_at')->nullable();
			$table->date('expire_at')->nullable();
			$table->ipAddress('ip6address')->nullable();
			$table->string('technology')->nullable();

			$table->integer('provided_supplier_broadband_id')->unsigned()->nullable();
			$table->foreign(['provided_supplier_broadband_id','site_id'])->references(['id','site_id'])->on('supplier_broadband');

			$table->integer('service_id')->unsigned();
			$table->foreign(['service_id','site_id'])->references(['id','site_id'])->on('services');

			$table->unique(['id','site_id']);
		});

		Schema::create('cost_broadband', function (Blueprint $table)
		{
			$table->integer('id',TRUE,TRUE);
			$table->integer('site_id')->unsigned();
			$table->integer('cost_id')->unsigned();
			$table->boolean('active');
			$table->integer('service_broadband_id')->unsigned()->nullable();
			$table->integer('supplier_broadband_id')->unsigned()->nullable();
			$table->date('start_at')->nullable();
			$table->date('end_at')->nullable();
			$table->float('base')->nullable();
			$table->float('excess')->nullable();
			$table->string('reference')->nullable();
			$table->text('notes')->nullable();

			$table->foreign(['cost_id','site_id'])->references(['id','site_id'])->on('costs');
			$table->foreign(['service_broadband_id','site_id'])->references(['id','site_id'])->on('service_broadband');
			$table->foreign(['supplier_broadband_id','site_id'])->references(['id','site_id'])->on('supplier_broadband');
		});

	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('cost_broadband');
		Schema::drop('service_broadband');
		Schema::drop('product_broadband');
		Schema::drop('supplier_broadband');
	}
};