<?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_domain',function (Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->boolean('active')->default(false);

			$table->string('product_id',16);
			$table->string('product_desc',128)->nullable();
			$table->float('base_cost');
			$table->float('setup_cost')->nullable();
			$table->integer('contract_term')->unsigned()->nullable();
			$table->text('whois_url')->nullable();
			$table->text('config')->nullable();

			$table->integer('site_id')->unsigned();
			$table->foreign(['site_id'])->references(['id'])->on('sites');
			$table->integer('tld_id')->unsigned();
			$table->foreign(['tld_id'])->references(['id'])->on('tlds');

			$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_domain',function (Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$table->boolean('active')->default(false);

			$table->string('name',64);
			$table->integer('contract_term')->unsigned()->nullable();

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

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

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

			$table->string('domain_name', 256);

			$table->date('expire_at')->nullable();

			$table->string('registrar_account', 32)->nullable();
			$table->string('registrar_username', 128)->nullable();
			$table->string('registrar_auth_password', 128)->nullable();
			$table->string('registrar_ns', 1024)->nullable();

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

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

			$table->integer('tld_id')->unsigned();
			$table->foreign(['tld_id'])->references(['id'])->on('tlds');

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

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('service_domain');
		Schema::drop('product_domain');
		Schema::drop('supplier_domain');
	}
};