<?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()
    {
		DB::statement('RENAME TABLE ab_domain_registrar TO domain_registrars');
		DB::statement('ALTER TABLE domain_registrars MODIFY active tinyint(1) NOT NULL');

		Schema::table('domain_registrars', function (Blueprint $table) {
			$table->datetime('created_at')->nullable()->after('id');
			$table->datetime('updated_at')->nullable()->after('created_at');

			$table->dropForeign('ab_domain_registrar_site_id_foreign');
			$table->dropIndex('ab_domain_registrar_id_site_id_index');

			$table->foreign(['site_id'])->references(['id'])->on('sites');
		});

		Schema::table('service_domain', function (Blueprint $table) {
			$table->foreign(['domain_registrar_id','site_id'])->references(['id','site_id'])->on('domain_registrars');
		});

		DB::statement('RENAME TABLE ab_host_server TO supplier_host_servers');
		DB::statement('ALTER TABLE supplier_host_servers MODIFY active tinyint(1) DEFAULT NULL,MODIFY debug tinyint(1) DEFAULT NULL');
		DB::statement('ALTER TABLE supplier_host_servers MODIFY id int unsigned auto_increment,MODIFY site_id int unsigned NOT NULL,MODIFY max_accounts int unsigned DEFAULT NULL');
		DB::statement('ALTER TABLE supplier_host_servers MODIFY notes longtext DEFAULT NULL');

		Schema::table('supplier_host_servers', function (Blueprint $table) {
			$table->datetime('created_at')->nullable()->after('id');
			$table->datetime('updated_at')->nullable()->after('created_at');

			$table->integer('supplier_id')->unsigned()->nullable();

			$table->foreign(['site_id'])->references(['id'])->on('sites');
			$table->foreign(['supplier_id'])->references(['id'])->on('suppliers');
		});

		DB::statement('ALTER TABLE service_host RENAME COLUMN host_server_id TO supplier_host_server_id');
		DB::statement('ALTER TABLE service_host MODIFY supplier_host_server_id int unsigned DEFAULT NULL');

		Schema::table('service_host', function (Blueprint $table) {
			$table->foreign(['supplier_host_server_id','site_id'])->references(['id','site_id'])->on('supplier_host_servers');
		});

		DB::statement('DROP TABLE ab_domain_tld');
		//delete from ab_host_server where id=0;
		//alter table service_host modify host_server_id int unsigned default null;
		//update service_host set host_server_id=NULL where host_server_id=0
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
		abort(500,'Cant go back');
    }
};