<?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_host',function (Blueprint $table)
		{
			$table->id();
			$table->timestamps();
			$table->integer('site_id')->unsigned();
			$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->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_host',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_host');

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

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

			$table->string('domain_name', 128)->nullable();
			$table->string('host_type',16)->nullable();
			$table->string('host_username',45)->nullable();
			$table->string('host_password',45)->nullable();
			$table->string('ftp_username',16)->nullable();
			$table->string('ftp_password',16)->nullable();
			$table->jsonb('server_data')->nullable();
			$table->date('expire_at')->nullable();

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

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

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

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

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('service_host');
		Schema::drop('product_host');
		Schema::drop('supplier_host');
	}
};