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

			$table->date('expire_at')->nullable();
			$table->string('company')->nullable();
			$table->string('address1', 128)->nullable();
			$table->string('address2', 128)->nullable();
			$table->string('city', 32)->nullable();
			$table->string('state', 32)->nullable();
			$table->string('zip', 16)->nullable();

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

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

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

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

		/*
		*/
		Schema::table('rtm', function(Blueprint $table) {
			$table->foreign(['account_id','site_id'])->references(['id','site_id'])->on('accounts');
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		/*
		*/
		Schema::table('rtm', function(Blueprint $table)
		{
			$table->dropForeign(['account_id','site_id']);
			$table->dropColumn('account_id');
		});

		Schema::drop('accounts');
	}
};