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

			$table->string('email', 256);
			$table->string('password', 191)->nullable();
			$table->string('remember_token', 100)->nullable();
			$table->string('title', 8)->nullable();
			$table->string('firstname', 128);
			$table->string('lastname', 128);
			$table->string('address1', 128)->nullable();
			$table->string('address2', 128)->nullable();
			$table->string('city', 128)->nullable();
			$table->string('state', 64)->nullable();
			$table->string('postcode', 16)->nullable();
			$table->boolean('emailable')->default(1);

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

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

			$table->integer('language_id')->unsigned()->nullable();
			$table->foreign(['language_id'])->references(['id'])->on('languages');

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

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

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

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

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